#!/bin/zsh

########
file_results=resultats
f_limits=limits.d
fileres=e_sedsL_Tm_vis_Q_epeff_epzz.xy
fepunt12=epunt12zz.xy
ffalla2=falla_xy.res
fvelocity=velocity.xy
fprin_stress=principal_stress.xy
fstress=azuhu_azdata.xy
fcorba1=corba.tmp
fcorba2=Points.tmp
f_resGLit=GLit.xy
f_Flit=Flit_Zmec_xy.res
f_screen=PANTALLA
f_rotation=res_rotation
file_Pav=res_Szzaver_Flit
file_TK=res_Tempe
file_ep_uhuru=res_StrainRateEff
file_HP=Heat_Production.res
file_tisc=results_tisc
file_TotalStrain=res_TotalStrain
#file_topodata=/home/ivone/Atlas/DATA/Atlas_Rif_9W30N.grd
file_topodata=/home/ivone/Atlas/DATA/Atlas_Rif_10W29N.grd

integer line_jump

echo '	1-Uhuru Paper;   2- Atlas - Alps;   3-Tibet -Zagros Model;   4-Parametritzacio Indenter'
#read Mod_selec
Mod_selec=4

if [ $Mod_selec -eq 1 ]	
then
   scale_1Fig=0.0085
   Bx_tics="a400f200"
   By_tics="a400f200"
   scale_2Fig=0.012
   scale_4Fig=0.0045
   Bx_tics_4="a200f100"
   By_tics_4="a200f100"
   facvel=5
fi
if [ $Mod_selec -eq 2 ]
then
   scale_1Fig=0.018	#0.017 Atlas	#0.012 # Alps
   Bx_tics="a50f10"
   By_tics="a50f10"
   scale_2Fig=0.0145
   scale_4Fig=0.009	#0.008 Atlas	#0.006 # Alps
   Bx_tics_4="a100f20"	#"a200f50" # Alps
   By_tics_4="a100f20"	#"a200f50" # Alps
   facvel=0.5		#5 #Alps
fi
if [ $Mod_selec -eq 3 ]
then
   scale_1Fig=0.0021
   Bx_tics="a1000f250"
   By_tics="a500f250"
   scale_2Fig=0.00215
   scale_4Fig=0.0012
   Bx_tics_4="a1500f250"
   By_tics_4="a1000f250"
   facvel=20
fi
if [ $Mod_selec -eq 4 ]
then
   scale_1Fig=0.002
   Bx_tics="a1000f250"
   By_tics="a1000f250"
   scale_2Fig=0.0018
   scale_4Fig=0.001
   Bx_tics_4="a1500f250"
   By_tics_4="a1000f250"
   facvel=20
fi

echo 'Selecciona:  0- Total erosionat i sedimentat  '
echo '             1- Stress directions	'
echo '             2- Strain Rate Tensor '
echo '             3- Viscosity	'
echo '             4- Effective strain rate (log)'
echo '             5- Velocity field + vertical strain rate'
echo '             6- Temperatura moho and Density of the lithospheric mantle'
echo '             7- Surface Heat Flow'
echo '             8- Bodies with different viscosity (+ crustal thickness + nodes/convective removal)'
echo '             9- sediment thickness'
echo '             10- Vertical Profile of the lithosphere structure'
echo '             11- Elevation + drainage + Rain, Sedimentation + Erosion	'
echo '             12- Velocities to the boundaries'
echo '             13- elevation, crustal thick, lithospheric thick, surface heat flow'
echo '             14- elevation and rivers, sediment, crustal and lithospheric mantle thicknesses'
echo '             15- Vorticity '
echo '             16- Moho Temperature  and Lithosphere thickness'
echo '             17- elevation + rivers,   erosion rate'

echo '             18- Total erosion or erosion rate '
echo '             19- Total Vertical Accumulated Strain '
echo '             20- Strength of the lithosphere (stress envelope integral), and initial bodies geometry'
echo '             21- Finite Rotation, puntuals'
echo '             22- Displacement of several points (+ crustal thickness)'
echo '             23- Temperature Cross profile'
echo '             24- Heat Production'
echo '             27- Depth average vertical stress over the plate'
echo '             28- GPE - Gravitational Potential Energy'

echo '             30- Find the Velocities to some points of the domain '
echo '             50- Flexure: elevation resulting from flexure + deflection'

echo '	Parametritzation:'
echo '             110- Strain localization, eros/sed volume  vs  other parameters'
echo '             111- Strain localization, eros/sed volume  vs  other parameters'

echo '	Temporal variations:'
echo '             125- Profiles of temporal variations'
echo '             126- Create a file with Temporal variations of max and min  (built *.t: elevation, crust, lithospheric_mantle and vertical strain rate)'
echo '	Results between different models:'
echo '             130- Difference between two models'
echo '             131- Difference between two models of the vertical strain rate AND total vertical strain accumulation'
echo '             135- Delta elevation, crust and mantle thickness versus Kerosdif'
echo '             136- Delta elevation, crust, mantle thickness and vertical strain rate with time, for different models'
echo '             140- Rotation - time, for a given point (Create files Rotation_time.xy with option 21)'
echo '             141- Delta Rotation between two models'

echo '             '
echo '             150- Temperature 1D'
echo '             '
echo '             To plot the present lithosphere structure from the file: Qe_sL_steady.res'
echo '             		use job: /home/ivone/src/Lithosphere_SteadyState/Qe_sL_output.job'


read selec


if [ $selec -eq 0 ]
then
awk '{	if($5=="Time") time=$7; \
	if($1=="noSed") {totaleros+=$3; totalsed+=$6; print time " My ; totaleros=", totaleros" ;  totalsed=", totalsed} }
	END {printf "totaleros= %.5e kg ;  totalsed= %.5e kg", totaleros/9.81, totalsed/9.81}' $f_screen
exit
fi

DIR_main=`pwd`

if [ $selec -eq 150 ]
then
file_T1D=fort.20
FILEPS=Temperature1D.ps
RTemp=0/1950/0/300
psbasemap -R$RTemp -P -X2 -JX16/-25 -Ba400f200:" Temperature (K)\
	":/a20f10:"Depth (km)":NwsE -K -V > $FILEPS 
awk '{if ($1!="#") {print $2,$1/1e3 } }' $file_T1D > file_T.tmp
#psxy file_T.tmp -R -JX -W9/255/0/0t25_15:1 -N -O -K -V >> $FILEPS
psxy file_T.tmp -JX -R -Sc.01 -G0 -N -M -O -V >> $FILEPS
rm -f file*.tmp
gv $FILEPS &
exit
fi


if [ $selec -lt 100 ]
then
ls -t resultats*
echo ' Extension to be plotted (resultats*)?'
read ext_res
graficsth <<END
$file_results$ext_res
END

awk '{print $0}' $f_limits | read x1 x2 y1 y2 x2vel y2vel 
awk '{if(NR==1) {print $4 } }' $fileres | read TIME_My

regio=$x1/$x2/$y1/$y2
regio_vel=0/$x2vel/0/$y2vel
Regio_graf=$regio_vel		## Alps: 0/1200/0/800	# $regio_vel

echo "  "
echo regio = $regio
echo regio_vel = $regio_vel
echo Regio_graf = $Regio_graf

xtit=$(($x2+0.8*$x2)) 
ytit=$(($y2-4*$x1)) 
ytitsota=$(($ytit-5*$x1)) 

fi

### Paletes de color

cat <<END> filePalette_cpt_color1.tmp
-8	0  0  100	-5	0  0  220	
-5	0  0  220	-3	0  100  220	
-3	0  100  220	-2	0  150  240	
-2	0  150  220	-1	0  200  240	
-1	0  200  240	-0.5	0  220  255	
-0.5	0  220  255	0	120  255  255	
0	75  180  155	0.100	75  200  125	
0.100	75  200  125	0.200	75  235   75	
0.200	75  235   75	0.500	125  255   75	
0.500	150  255   75	1	175  255   75	
1	200  255   75	1.4	200  200   75  
1.4	210  190  110	1.8	210     190     110
1.8	160   140  80	2.2	160     140     80
2.2	255  255  255	4	255  255  255
END
cat <<END> filePalette_cpt_color2.tmp
-10  130   60  170	-8   130   60  170
-8   130  100  180	-6   130  100  180
-6   90    90  180	-5   90    90  180
-5   70   115  170	-4   70   115  170
-4   90   150  190	-3   90   150  190
-3   100  180  210	-2   100  180  210
-2   150  215  225	-1   150  215  225
-1   190  230  240	-0.2    190  230  240
-0.2    210  240  240	0       210  240  240
0       100  200  100	0.2     100  200  100
0.2     160  255  100	0.5     160  255  100
0.5     255  255  120	1    255  255  120
1    255  220   60	2    255  220   60
2    210  180  110	3    210  180  110
3    210  150   80	4    210  150   80
4    225  225  225	8    225  225  225
END

cat <<END> filePalette_cpt_color2-topo.tmp
# -10	130   60  170   -8	130   60  170
# -8	130  100  180   -6	130  100  180
# -6	90    90  180   -5	90    90  180
# -5	70   115  170   -4	70   115  170
# -4	90   150  190   -3	90   150  190
-3   100  180  210	-2   100  180  210
-2   150  215  225	-1   150  215  225
-1   190  230  240	-0.2    190  230  240
-0.2    210  240  240	0       210  240  240
0       100  200  100	0.2     100  200  100
0.2     160  255  100	0.5     160  255  100
0.5     255  255  120	1    255  255  120
1    255  220   60	2    255  220   60
2    210  180  110	3    210  180  110
3    210  150   80	4    210  150   80
4    235  235  235	8    235  235  235
B   100  180  210
F   255  255  255
END

cat <<END> filePalette_cpt_BW.tmp
0  	0       0       0       0.1	0       0       0
0.1 	28      28      28      0.3	28      28      28
0.3 	57      57      57      0.5	57      57      57
0.5 	85      85      85      0.8	85      85      85
0.8	113     113     113     1.0	113     113     113
1.0	150     150     150     1.4	150     150     150
1.4	180     180     180     1.8	180     180     180
1.8	210     210     210     2.2	210     210     210
2.2	240     240     240     2.6	240     240     240
2.6	255     255     255     3.5	255     255     255
END

cat <<END> filecpt_Sediment-Erosio.tmp					# erosion + sediment
-15	255	160	255	-8	255	160	255
-8	255	0	255	-5	255	0	255
-5	180	0	255	-3	180	0	255
-3	57	57	220	-2	57	57	220
-2	113	113	255	-1	113	113	255
-1	170	170	255	-0.5	170	170	255
-0.5	255	255	255	0	255	255	255
0	255	255	255	0.2	255	255	200
0.2	255	255	80	0.5	255	255	80
0.5	255	200	50	1	255	200	50
1.0	255	80	0	2	255	80	0
2	200	0	0	3	200	0	0
3	100	0	0	8	100	0	0
END


#####################


if [ $selec -eq 1 ]		#############	Stress Direction and Tectonic Regime	########
then				## Compressio negatiu / Extensio positiu.
FILEPS=stress_modul.ps		## Esfors horizontal :  Compressio = una barra / Extensio = una fletxa
rm -f $FILEPS			##  LONGITUT DE LA FLETXA RELACIONADA AMB LA MAGNITUT

Regio_graf=$regio	#$x1/2500/500/3800	#$regio

Label_stress_MPa=20
facstress=1.5e7
line_jump=$((0.8*$x2vel/$x1))	# 25

echo 800 -410  0  $Label_stress_MPa  > file_label.tmp
awk '{print $1,$2,$3,$4*1e6/facstress,0}' facstress=$facstress file_label.tmp > file_pstress.tmp

awk ' BEGIN {linia=1}{if(NR==linia) {print $1,$2,$3,$4/facstress,$5/facstress,$6/facstress,$7; \
	linia=linia+line_jump} }' facstress=$facstress line_jump=$line_jump $fprin_stress >> file_pstress.tmp

awk '{if($7=="NF" && $4>=0) {print ($1,$2,$3,$4)} }' file_pstress.tmp > file_stress_NF_tensio.tmp
awk '{if($7=="NF" && $4>=0) {print ($1,$2,($3+180),$4)} }' file_pstress.tmp >> file_stress_NF_tensio.tmp
awk '{if($7=="NF" && $4<0) {print ($1,$2,$3,-$4)} }' file_pstress.tmp > file_stress_NF_comp.tmp
awk '{if($7=="NF" && $4<0) {print ($1,$2,($3+180),-$4)} }' file_pstress.tmp >> file_stress_NF_comp.tmp
awk '{if($7=="NS" && $4>=0) {print ($1,$2,$3,$4)} }' file_pstress.tmp > file_stress_NS_tensio.tmp
awk '{if($7=="NS" && $4>=0) {print ($1,$2,($3+180),$4)} }' file_pstress.tmp >> file_stress_NS_tensio.tmp
awk '{if($7=="NS" && $4<0) {print ($1,$2,$3,-$4)} }' file_pstress.tmp > file_stress_NS_comp.tmp
awk '{if($7=="NS" && $4<0) {print ($1,$2,($3+180),-$4)} }' file_pstress.tmp >> file_stress_NS_comp.tmp
awk '{if($7=="SS" && $4>=0) {print ($1,$2,$3,$4)} }' file_pstress.tmp > file_stress_SS_tensio.tmp
awk '{if($7=="SS" && $4>=0) {print ($1,$2,($3+180),$4)} }' file_pstress.tmp >> file_stress_SS_tensio.tmp
awk '{if($7=="SS" && $4<0) {print ($1,$2,$3,-$4)} }' file_pstress.tmp > file_stress_SS_comp.tmp
awk '{if($7=="SS" && $4<0) {print ($1,$2,($3+180),-$4)} }' file_pstress.tmp >> file_stress_SS_comp.tmp
awk '{if($7=="TS" && $4>=0) {print ($1,$2,$3,$4)} }' file_pstress.tmp > file_stress_TS_tensio.tmp
awk '{if($7=="TS" && $4>=0) {print ($1,$2,($3+180),$4)} }' file_pstress.tmp >> file_stress_TS_tensio.tmp
awk '{if($7=="TS" && $4<0) {print ($1,$2,$3,-$4)} }' file_pstress.tmp > file_stress_TS_comp.tmp
awk '{if($7=="TS" && $4<0) {print ($1,$2,($3+180),-$4)} }' file_pstress.tmp >> file_stress_TS_comp.tmp
awk '{if($7=="TF" && $4>=0) {print ($1,$2,$3,$4)} }' file_pstress.tmp > file_stress_TF_tensio.tmp
awk '{if($7=="TF" && $4>=0) {print ($1,$2,($3+180),$4)} }' file_pstress.tmp >> file_stress_TF_tensio.tmp
awk '{if($7=="TF" && $4<0) {print ($1,$2,$3,-$4)} }' file_pstress.tmp > file_stress_TF_comp.tmp
awk '{if($7=="TF" && $4<0) {print ($1,$2,($3+180),-$4)} }' file_pstress.tmp >> file_stress_TF_comp.tmp
awk '{if($5>0) {print ($1,$2,$3+90,$5)} }' file_pstress.tmp > file_stress2_tensio.tmp
awk '{if($5>0) {print ($1,$2,$3+270,$5)} }' file_pstress.tmp >> file_stress2_tensio.tmp
awk '{if(NR==1) {print $1,$2,$3,$4 } }' file_pstress.tmp >> file_stress2_tensio.tmp 
awk '{if(NR==1) {print $1,$2,$3+180,$4 } }' file_pstress.tmp >> file_stress2_tensio.tmp
#cat<<END>> file_stress2_tensio.tmp
#800 -250 -45  0.5
#800 -250 135  0.5
#END

#awk '{if($1!="#"){print $1,$2,$5} }' $fileres > file_xycont.tmp  			## crust
#contour_text=" contours: crustal thickness (km)" 					##  crust
awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xycont.tmp  			## elevation
contour_text=" contours: elevation (km)" 					##  elevation
xyz2grd file_xycont.tmp -R$regio_vel -Gfile_crust_grd.tmp -I$x1/$y1 			## grid
#scale_1Fig=0.0053
pstext titol.tmp -P -R$regio_vel -X1 -Y19 -Jx.011 -N -K -V > $FILEPS
psbasemap -Y-11 -X1 -R$Regio_graf -Jx$scale_1Fig -B$Bx_tics/$By_tics:" ":WNes -K -O -V >> $FILEPS
echo " "
echo " grdimage of the teconic regime?  1-Yes"
read timage
if [ $timage -eq 1 ]	# grdimage of the tectonic regime 
then
awk '{if(NR>linia && $7=="NF") {print $1,$2,0.5} }' $fprin_stress > file_TectonicRegime.tmp
awk '{if(NR>linia && $7=="NS") {print $1,$2,1.5} }' $fprin_stress >> file_TectonicRegime.tmp
awk '{if(NR>linia && $7=="SS") {print $1,$2,2.5} }' $fprin_stress >> file_TectonicRegime.tmp
awk '{if(NR>linia && $7=="TS") {print $1,$2,3.5} }' $fprin_stress >> file_TectonicRegime.tmp
awk '{if(NR>linia && $7=="TF") {print $1,$2,4.5} }' $fprin_stress >> file_TectonicRegime.tmp
cat <<END> file_cpt.tmp
0	130 255 130	1	130 255 130
1	220 220 100	2	220 220 100
2	255 130 130	3	255 130 130
3	250 130 255	4	250 130 255
4	130 130 255	5	130 130 255
END
xyz2grd file_TectonicRegime.tmp -R$regio -Gfile_grd.tmp -I$x1/$y1

grdimage file_grd.tmp -Jx -R$Regio_graf -Cfile_cpt.tmp -O -K -V >> $FILEPS
psscale -Cfile_cpt.tmp -L -D3/-2.5/5/.3h -B:" NF    NS    SS   TS    TF ": -O -K -V >> $FILEPS

grdcontour file_crust_grd.tmp -R$Regio_graf -C0.5 -A1f8 -G3/8 -W1/0 -Jx -Bnsew -O -K -V >> $FILEPS ## contours

cat file_stress_NF_comp.tmp file_stress_NS_comp.tmp file_stress_SS_comp.tmp file_stress_TS_comp.tmp file_stress_TF_comp.tmp > file_stress_comp.tmp
psxy file_stress_comp.tmp -R -N -K -O -Jx -Sv.05/0/0 -G0/0/0 >> $FILEPS

else

grdcontour file_crust_grd.tmp -R$Regio_graf -C0.5 -A1f8 -G3/8 -W1/0 -Jx -Bnsew -O -K -V >> $FILEPS ## contours

psxy file_stress_NF_tensio.tmp -R -K -O -N -Jx -Sv.0001/0.08/0.05 -W2/0/255/0 -G0/255/0 >> $FILEPS
psxy file_stress_NF_comp.tmp -R -K -O -N -Jx -Sv.05/0/0 -G0/255/0 -W2/0/255/0 >> $FILEPS
psxy -R -O -K -N -Jx -Sv.05/0/0 -G0/255/0 -W2/0/255/0 -V <<END>> $FILEPS
1300 -150 -45  0.5
1300 -150 135  0.5
END
psxy file_stress_NS_tensio.tmp -R -K -O -N -Jx -Sv.0001/0.08/0.05 -W2/90/90/0 -G90/90/0 >> $FILEPS
psxy file_stress_NS_comp.tmp -R -K -O -N -Jx -Sv.05/0/0 -G90/90/0 -W2/90/90/0 >> $FILEPS
psxy -R -O -K -N -Jx -Sv.05/0/0 -G90/90/0 -W2/90/90/0 -V <<END>> $FILEPS
1300 -230 -45  0.5
1300 -230 135  0.5
END
psxy file_stress_SS_tensio.tmp -R -K -O -N -Jx -Sv.0001/0.08/0.05 -W2/220/0/0 -G220/0/0 >> $FILEPS
psxy file_stress_SS_comp.tmp -R -K -O -N -Jx -Sv.03/0/0 -G220/0/0 -W2/220/0/0 >> $FILEPS
psxy -R -O -K -N -Jx -Sv.03/0/0 -G220/0/0 -W2/220/0/0 -V <<END>> $FILEPS
1300 -310 -45  0.5
1300 -310 135  0.5
END
psxy file_stress_TS_tensio.tmp -R -K -O -N -Jx -Sv.0001/0.08/0.05 -W2/120/0/170 -G120/0/170 >> $FILEPS
psxy file_stress_TS_comp.tmp -R -K -O -N -Jx -Sv.02/0/0 -G120/0/170 -W2/120/0/170 >> $FILEPS
psxy -R -O -K -N -Jx -Sv.02/0/0 -G120/0/170 -W2/120/0/170 -V <<END>> $FILEPS
1300 -390 -45  0.5
1300 -390 135  0.5
END
psxy file_stress_TF_tensio.tmp -R -K -O -N -Jx -Sv.0001/0.08/0.05 -W2/0/0/255 -G0/0/255 >> $FILEPS
psxy file_stress_TF_comp.tmp -R -K -O -N -Jx -Sv.02/0/0 -G0/0/255 -W2/0/0/255 >> $FILEPS
psxy -R -O -K -N -Jx -Sv.02/0/0 -G0/0/255 -W2/0/0/255 -V <<END>> $FILEPS
1300 -470 -45  0.5
1300 -470 135  0.5
END
fi

psxy $fcorba1 -R -O -K -M -N -Jx -W5/100/100/100 -V >> $FILEPS
#psxy $fcorba2 -R -O -K -N -Jx -Sc.1 -V >> $FILEPS

#psxy file_stress2_tensio.tmp -R -K -O -N -Jx -Sv.0001/0.08/0.05 -W2/50/50/50 >> $FILEPS
psxy file_stress2_tensio.tmp -R -N -K -O -Jx -Sv.08/0.25/0.13 -W2/0/0/0 -G0/170/0  >> $FILEPS

pstext -R -Jx -O -K -N -V <<END>> $FILEPS
800 -400 12 0 0 2  $Label_stress_MPa MPa
30 -110 12 0 0 0  $contour_text
END
pstext -R -Jx -O -N -V <<END>> $FILEPS
800 -140 12 0 1 2  Horizontal Principal Stress
1350 -170 12 0 4 0  NF: Normal Fault
1350 -250 12 0 4 0  NS: NF with SS
1350 -330 12 0 4 0  SS: Strike Slip
1350 -410 12 0 4 0  TS: TF with SS
1350 -490 12 0 4 0  TF: Thrust Fault
1350 -570 12 0 4 0  Extension
END
rm -f file_*.tmp
fi

if [ $selec -eq 2 ]		#############	Strain Rate Tensor	########
then				## Compressio negatiu (barra) / Extensio positiu (fletxa)
FILEPS=StrainRateTensor.ps	## LONGITUT DE LA FLETXA RELACIONADA AMB LA MAGNITUT
rm -f $FILEPS

echo " "
timage=1
line_jump=25	#18	#101	#151	#13	#35		#15
Label_SR_yr=4e-8	#0.3e-8
fac_srate=1e16	#0.1e16		# 1e16
strain_min=0.1
#strain_min_SI=$(($strain_min*$fac_srate)) 
awk '{printf "%.3f ", (strain_min*fac_srate)/1e15 }' strain_min=$strain_min fac_srate=$fac_srate $f_limits | read strain_min_SI

echo " Plot all symbols with the same length, "
echo " 		no proportional to the modulus?  1-Yes"
read tsymb

awk '{if(NR==1){print Label_SR_yr/3.1536e7} }' Label_SR_yr=$Label_SR_yr $fepunt12 | read Label_SR
y_symbol=$(($y1*13)) 
y_symbol_text=$(($y1*12)) 

echo 800 -$y_symbol  0  $Label_SR  > file_label.tmp
awk '{print $1,$2,$3,$4*fac_srate,0}' fac_srate=$fac_srate file_label.tmp > file_teta_12.tmp
awk ' BEGIN {linia=1}{if(NR==linia) {print $1,$2,90-$6,$3*fac_srate,$4*fac_srate; \
	linia=linia+line_jump} }' fac_srate=$fac_srate line_jump=$line_jump $fepunt12 >> file_teta_12.tmp


awk '{if($4>strain_min) {print $1,$2,$3,$4} }' strain_min=$strain_min file_teta_12.tmp > file_extension.tmp
awk '{if($4>strain_min) {print $1,$2,$3+180,$4} }' strain_min=$strain_min file_teta_12.tmp >> file_extension.tmp
awk '{if($5>strain_min) {print $1,$2,$3+90,$5} }' strain_min=$strain_min file_teta_12.tmp >> file_extension.tmp
awk '{if($5>strain_min) {print $1,$2,$3+270,$5} }' strain_min=$strain_min file_teta_12.tmp >> file_extension.tmp

#awk '{if(NR==1) {printf "%3.0f ", fac_srate/3.1536e7} }'  fac_srate=$fac_srate $fepunt12  | read  tic_fac
#awk '{if(NR==1) {print fac_srate/3.1536e7} }'  fac_srate=$fac_srate $fepunt12  | read  tic_fac

#echo $fac_srate/31536000 | bc -l | read tic_fac

awk '{if($4<strain_min) {print $1,$2,$3,-$4} }' strain_min=$strain_min file_teta_12.tmp > file_compression.tmp
awk '{if($4<strain_min) {print $1,$2,$3+180,-$4} }' strain_min=$strain_min file_teta_12.tmp >> file_compression.tmp
awk '{if($5<strain_min) {print $1,$2,$3+90,-$5} }' strain_min=$strain_min file_teta_12.tmp >> file_compression.tmp
awk '{if($5<strain_min) {print $1,$2,$3+270,-$5} }' strain_min=$strain_min file_teta_12.tmp >> file_compression.tmp

Regio_graf=$regio	#$x1/2500/500/3800	#$regio
#scale_1Fig=0.0053
pstext titol.tmp -P -R$regio_vel -Y21 -JX10/7 -N -K -V > $FILEPS
psbasemap -Y-13 -X1 -R$Regio_graf -Jx$scale_1Fig -B$Bx_tics/$By_tics -K -O -V >> $FILEPS

if [ $timage -eq 1 ]	# grdimage of the tectonic regime 
then
awk '{if(NR>linia && $7=="NF") {print $1,$2,0.5} }' $fprin_stress > file_TectonicRegime.tmp
awk '{if(NR>linia && $7=="NS") {print $1,$2,1.5} }' $fprin_stress >> file_TectonicRegime.tmp
awk '{if(NR>linia && $7=="SS") {print $1,$2,2.5} }' $fprin_stress >> file_TectonicRegime.tmp
awk '{if(NR>linia && $7=="TS") {print $1,$2,3.5} }' $fprin_stress >> file_TectonicRegime.tmp
awk '{if(NR>linia && $7=="TF") {print $1,$2,4.5} }' $fprin_stress >> file_TectonicRegime.tmp
cat <<END> file_cpt.tmp
0	180 255 180	1	180 255 180
1	240 240 130	2	240 240 130
2	255 150 150	3	255 150 150
3	250 130 255	4	250 130 255
4	130 130 255	5	130 130 255
END
xyz2grd file_TectonicRegime.tmp -R$regio -Gfile_grd.tmp -I$x1/$y1

grdimage file_grd.tmp -Jx -R$Regio_graf -Cfile_cpt.tmp -O -K -V >> $FILEPS
psscale -Cfile_cpt.tmp -D10/-3.5/5/.3h -B:" NF    NS    SS   TS    TF ": -O -K -V >> $FILEPS
fi

#awk '{if($1!="#"){print $1,$2,$5} }' $fileres > file_xycont.tmp  			## crust
#contour_text=" contours: crustal thickness (km)" 					##  crust
awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xycont.tmp  			## elevation
contour_text=" contours elevation (km)" 					##  elevation
xyz2grd file_xycont.tmp -R$regio_vel -Gfile_crust_grd.tmp -I$x1/$y1 			## 
grdcontour file_crust_grd.tmp -R$Regio_graf -C0.5 -A1f10 -G5/8 -W4/50 -Jx  -B$Bx_tics:" \
	$contour_text strain rate > $strain_min_SI x10@+15@+ s@+-1@+":S -O -K -V >> $FILEPS ## contours

if [ $tsymb -eq 1 ]	# symbols not scaled with modulus 
then
   mv file_compression.tmp   file_tmp.tmp
   awk '{print $1,$2,$3,0.35}' file_tmp.tmp > file_compression.tmp
   mv file_extension.tmp   file_tmp.tmp
   awk '{print $1,$2,$3,0.35}' file_tmp.tmp > file_extension.tmp
else
pstext -R$Regio_graf -Jx -O -K -N -V <<END>> $FILEPS
800 -$y_symbol_text 12 0 0 2  $Label_SR_yr yr@+-1@+ = $Label_SR s@+-1@+
END
fi

#psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
psxy $fcorba1 -R -M -N -Jx -W5/0/0/0t35_5_0_0:5 -V -O -K >> $FILEPS
#psxy $fcorba2 -R$Regio_graf -N -Jx -Sc.1 -V -O -K >> $FILEPS

psxy file_compression.tmp -R$Regio_graf -Bnew -Jx -Sv.05/0/0 -G0/0/0 -K -O -V >> $FILEPS
psxy file_extension.tmp -R$Regio_graf -Jx -Sv.08/0.25/0.13 -W2/0/0/0 -G0/170/0 -O -V >> $FILEPS

fi


if [ $selec -eq 3 ]		#############    Viscosity
then
FILEPS=viscositat.ps
rm -f $FILEPS

pstext titol.tmp -R$regio_vel -Y14 -X3 -JX10/5.5 -N -K -V > $FILEPS
awk '{if($1!="#") {print $1,$2,log($8)/log(10) } }' $fileres > viscositat.tmp
#awk '{if($1!="#") {print $1,$2,$8/1e22 } }' $fileres > viscositat.tmp
psbasemap -Y-10 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:" $DIR_main \
	":/$By_tics:" y (km)":WesN -V -K -O >> $FILEPS
xyz2grd viscositat.tmp -R -Gvis_grd.tmp -I$x1/$y1
#############   Paleta Blanc i Negre  ############
cat <<END> file_palette_cpt.tmp
21 	255     180     255	21.1 	255     180     255
21.1 	240     0       240	21.4 	240     0       240
21.4 	140     0       255	21.8	140     0       255
21.8 	0       28      255	22.2	0       28      255
22.2 	0       170     255	22.6	0       170     255
22.6 	0       255     199	23	0       255     199
23 	0	255     56	23.4	0       255     56
23.4 	227     255     0	23.8	227     255     0
23.8 	255     142     0	24.2	255     142     0
24.2	255     0       0	24.6	255     0       0
24.6	180     0       0	24.9	180     0       0
24.9	120     0       0	25	120     0       0
END
################################################################
#grdsample vis_grd.tmp -Gfile_grd_sample.tmp -N102/102 -V -R
cat <<END> file_palette_cpt.tmp
20 120 0 0 21 120 0 0
END
makecpt -Cjet -T21/25/0.25 -I >> file_palette_cpt.tmp 
cat <<END>> file_palette_cpt.tmp
25 0 0 120 26.5 0 0 120
END
#makecpt -Cjet -T1/49/4 > file_palette_cpt.tmp 
#cat <<END>> file_palette_cpt.tmp
#49 120 0 0 1000 120 0 0
#END
#grd2cpt vis_grd.tmp > file_palette_cpt.tmp

#makecpt -Chaxby -T22.92/23.2/0.02 > file_palette_cpt.tmp
#makecpt -Cgray -T22.84/23.15/0.02 > file_palette_cpt.tmp

grdimage vis_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D8/-1.5/20/.3h -B:"Viscosity,  log (@~h@~ @-eff@-(Pa.s))": -O -K -V >> $FILEPS
#psscale -Cfile_palette_cpt.tmp -L -D8/-1.5/20/.3h -B:"Viscosity,  @~h@~ @-eff@-(x 10@+22@+ Pa.s)": -O -K -V >> $FILEPS
psxy $fcorba1 -R -O -K -N -Jx -Sc0.2 -G0 -W1/255 -N -M -V >> $FILEPS
psxy $fcorba2 -R -O -K -Jx -Sc0.2 -G255 -W1/0 -N -V >> $FILEPS
#grdcontour vis_grd.tmp -C0.05 -A0.1 -G2/8 -O -K -Bnsew -Jx -R -V >> $FILEPS

awk '{if($1!="#"){print $1,$2,$5 } }' $fileres > file_xycrust.tmp  # crust
xyz2grd file_xycrust.tmp -R$regio_vel -Gfile_crust_grd.tmp -I$x1/$y1
contour_text="contours crustal thickness (km)"
grdcontour file_crust_grd.tmp -Jx -R -B$By_tics:" $contour_text ":ewnS -C10 -A20 -G8/8 -W1/0 -O -V >> $FILEPS

#pstext -R -Jx -O -N -V <<END>> $FILEPS
#0 -200 11 0 0 0 $contour_text
#END
rm -f viscositat.tmp vis_*.tmp file_*.tmp

#echo ' convert to jpg? 1-Yes'
#read Yjpg
#if [ $Yjpg -eq 1 ]
#then
#convert $FILEPS Viscosity_$ext_res".jpg"
#fi

fi

if [ $selec -eq 4 ]		### Effective strain rate
then
echo "Effective strain rate"
FILEPS=StrainRate_eff.ps
rm -f $FILEPS

#label_txt="log (maxim strain rate(s@+-1@+))"
#awk '{print $1,$2,(log((sqrt($3*$3)+sqrt($4*$4)+sqrt($5*$5))/2))/log(10)} ' $fepunt12 > file_maxep.tmp	# Maximum strain rate
label_txt="log ( strain rate(s@+-1@+) )"		##(10@+16@+ s@+-1@+)

awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xy_cont.tmp	# elevation
xyz2grd file_xy_cont.tmp -R -GfileElev_grd.tmp -I$x1/$y1
contour_text=" contours elevation (km)"

awk '{if($1!="#"){print $1,$2,log($10)/log(10)} }' $fileres > file_maxep.tmp		   # effective strain rate	from velocities	
#awk '{if($1!="#"){print $1,$2,log($3)/log(10)} }' $file_ep_uhuru$ext_res > file_maxep.tmp  # effective strain rate from uhuru

pstext titol.tmp -R$regio_vel -Y14.7 -X3 -JX10/5.5 -N -K -V > $FILEPS
psbasemap -Y-10.7 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:"  effective strain rate \
	":/$By_tics:" y (km)":WeSn -V -K -O >> $FILEPS

xyz2grd file_maxep.tmp -R -Gfile_rate_grd.tmp -I$x1/$y1
#############   Paleta Blanc i Negre  ############
cat <<END> file_rate_cpt.tmp
-20	10	10	10 	 -18	20	20	20 
-18	55	55	55	 -17 	55	55	55  
-17	90	90	90	 -16.75	90	90	90
-16.75	120	120	120	 -16.5	120	120	120
-16.5	160	160	160	 -16.25	160	160	160
-16.25	200	200	200	 -15.5	200	200	200
-15.5	250	250	250	 -14	255	255	255
END
###########################
#grd2cpt file_rate_grd.tmp -V > file_rate_cpt.tmp
#makecpt -Chaxby -T-17.9/-14/0.3 > file_rate_cpt.tmp
makecpt -Cjet -T-18/-15/0.25 > file_rate_cpt.tmp 
cat <<END>> file_rate_cpt.tmp
-15 120 0 0 -14 120 0 0
END
#grdsample file_rate_grd.tmp -Gfile_grd_sample.tmp -N102/102 -V -R
grdimage file_rate_grd.tmp -Jx -R -O -K -Cfile_rate_cpt.tmp -V >> $FILEPS
psscale -Cfile_rate_cpt.tmp -L -D11/-1.5/22/.3h -B:" $label_txt $contour_text": -O -K -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -N -L -Bnsew -Jx -W5/100/0/0t35_35_0_0:5 -V >> $FILEPS
psxy $fcorba2 -R -O -K -Jx -Sc0.2 -G255 -W1/0 -N -V >> $FILEPS
#grdcontour file_rate_grd.tmp -C0.5 -A0.5 -G2/8 -O -K -Jx -R -V >> $FILEPS

grdcontour fileElev_grd.tmp -Jx -R -C1 -A2 -G2.5/8 -W1/0 -O -K -V >> $FILEPS

#####   used on viscosity relation
psbasemap -X12 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:" strain rate used on viscosity":/$By_tics:" y (km)":wESn -V -K -O >> $FILEPS

awk '{if($1!="#"){print $1,$2,log($4)/log(10)} }' $file_ep_uhuru$ext_res > file_maxep.tmp  # strain rate used for viscosity
xyz2grd file_maxep.tmp -R -Gfile_rate_grd.tmp -I$x1/$y1
#grd2cpt file_rate_grd.tmp -V > file_rate_cpt.tmp
#makecpt -Chaxby -T-17.9/-14/0.3 > file_rate_cpt.tmp
#makecpt -Cjet -T-18/-14/0.3 > file_rate_cpt.tmp 
#cat <<END>> file_rate_cpt.tmp
#25 120 0 0 32 120 0 0
#END
grdimage file_rate_grd.tmp -Jx -R -O -K -Cfile_rate_cpt.tmp -V >> $FILEPS
#psscale -Cfile_rate_cpt.tmp -L -D8/-1.5/16/.3h -B:" $label_txt ": -O -K -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -N -L -Bnsew -Jx -W5/100/0/0t35_35_0_0:5 -V >> $FILEPS
psxy $fcorba2 -R -O -K -Jx -Sc0.2 -G255 -W1/0 -N -V >> $FILEPS
#grdcontour file_rate_grd.tmp -C0.5 -A0.5 -G2/8 -O -Jx -R -V >> $FILEPS

grdcontour fileElev_grd.tmp -Jx -R -C1 -A2 -G2.5/8 -W1/0 -O -V >> $FILEPS


rm -f file_*.tmp
fi


if [ $selec -eq 5 ]
then
###########################################################################
###################  CAMP DE VELOCITATS ###################################
echo VELOCITATS
FILEPS=velocitat.ps
rm -f $FILEPS
#min=0.3
#facvel=0.5		#5 #Alps		#20 # Tibet
fvel_mod=1
vmin=0   	#colapse orogenic
#wc $fvelocity | read nrow a b c
integer line_jump
line_jump=$((0.7*$x2vel/$x1))
echo $line_jump
#line_jump=133	#166	#18
echo ' '
echo ' Colours with the velocity modulus or the vertical strain rate? 1-velocity' 
read tgrd
#tgrd=1		#grdimage: 1-velocity modulus   =! vertical strain rate 

x_symbol=$((-3*$x1)) 
pstext titol.tmp -R$regio_vel -Y15 -X3.5 -JX10/5.5 -N -K -V > $FILEPS
awk '{if($3!=0 || $4!=0){print $1,$2,(180/3.1416)*atan2($4,$3),sqrt($3*$3+$4*$4)} \
	else {print $1,$2,0,0}}' $fvelocity > file_az_vel.tmp

awk '{if($4>=vmin){print ($1,$2,$3,$4/facvel)} }' \
	facvel=$facvel vmin=$vmin file_az_vel.tmp > file_vel1.tmp
awk ' BEGIN {linia=1}
       {if(NR==linia) {print $1,$2,$3,$4; linia=linia+line_jump} }' line_jump=$line_jump file_vel1.tmp > file_vel.tmp
cat <<END>> file_vel.tmp
$x_symbol -$y1 180 2
END
tic_facvel=$(($facvel*2)) 
       

if [ $tgrd -eq 1 ]				## velocity modulus
then
#text_scale="vel(mm/year) / $fvel_mod"
text_scale="velocity (mm/year)"
#awk '{print $1,$2,$4/fvel_mod }' fvel_mod=$fvel_mod file_az_vel.tmp > file_zxy.tmp 
awk '{print $1,$2,$4 }' file_az_vel.tmp > file_zxy.tmp 
xyz2grd file_zxy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
cat <<END> file_palette_cpt.tmp
0	113     0       200     5	113     0       200
5	0       28      255     10	0       28      255
10	0       170     255     16	0       170     255
16	0       255     199     22	0       255     199
22	0       255     56      28	0       255     56
28	85      255     0       34	85      255     0
34	227     255     0       40	227     255     0
40	250     200     0       46	250     200     0
46	255     130     0       49	255     130     0
49	255     0       0       51	255     0       0
51	200     0       0       80	200     0       0
END
makecpt -Cjet -I -T0/50/2.5 > file_palette_cpt.tmp 
#makecpt -Cjet -I -T0/2.2/0.1 > file_palette_cpt.tmp 
#grd2cpt file_grd.tmp > file_palette_cpt.tmp
else						##  vertical strain rate
text_scale="vertical strain rate (10@+-16@+ s@+-1@+)"
awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres > file_zxy_1.tmp		
awk '{if($3>1.e-4 || $3<-1.e-4){print $1,$2,$3} else {print $1,$2,0}}' file_zxy_1.tmp > file_zxy.tmp		
xyz2grd file_zxy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
cat <<END> file_palette_cpt.tmp
-20	0	0	80	-10	0	0	80
-10	0	0	150	-3	0	0	150
-3	57	57	220	-2	57	57	220
-2	113	113	255	-1	113	113	255
-1	170	170	255	-0.5	170	170	255
-0.5	227	227	255	0	227	227	255
0	255	227	227	0.5	255	227	227
0.5	255	170	170	1	255	170	170
1	255	113	113	2	255	113	113
2	220	57	57	3	220	57	57
3	150	0	0	10	150	0	0
10	80	0	0	20	80	0	0
END

#grd2cpt file_grd.tmp > file_palette_cpt.tmp
#makecpt -Chaxby -T1.3/4.1/0.2 > file_palette_cpt.tmp
#makecpt -Cgray -T1.29/3.99/0.18 > file_palette_cpt.tmp
#make_polar_cpt.job <<END   ## Crea la paleta de color
#file_zxy.tmp
#END
fi
psbasemap -Y-10 -X1 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:" $DIR_main ":/$By_tics:"  ":N -O -K -V >> $FILEPS
grdimage file_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D6.3/-2/13/.3h -B:" $text_scale ": -O -K -V >> $FILEPS

awk '{if($1!="#"){print ($1,$2,$5)} }' $fileres > file_xycrust.tmp  # crust
xyz2grd file_xycrust.tmp -R$regio_vel -Gfile_crust_grd.tmp -I$x1/$y1
contour_text=" contours crustal thickness (km)"
#grdcontour file_crust_grd.tmp -Jx -R -B$Bx_tics:" velocity  >  $vmin mm/any,   $contour_text ":/$By_tics:"y (km)":EnwS -C10 -G8/8 -W1/0 -O -K -V >> $FILEPS
grdcontour file_crust_grd.tmp -Jx -R -B$Bx_tics:" $contour_text ":/$By_tics:"y (km)":EnwS -C10 -G8/8 -W1/0 -O -K -V >> $FILEPS

#grdcontour file_grd.tmp -C1 -A2 -G1.5/8 -W1/0 -O -K -Jx -Bnsew -R -V >> $FILEPS
#contour_text=" contours: vertical strain rate (s@+-1@+)"

psxy $fcorba1 -R -O -K -M -L -Jx -W10/0/0/0t35_35_0_0:5 -N -V >> $FILEPS
psxy file_vel.tmp -R -Bnsew -N -Jx -Sv.08/0.38/0.15n0.5 -G0 -W2/255 -K -O -V >> $FILEPS
#psxy $fcorba2 -R -O -K -Jx -Sc0.2 -G255 -W1/0 -N -V >> $FILEPS

pstext -R -Jx -O -N -V <<END>> $FILEPS
$x_symbol 0 11 0 0 3 $tic_facvel mm/year
#400 -200 11 0 0 0 $contour_text
END

rm -f file_*.tmp
fi


if [ $selec -eq 6 ]   ###  Temperature moho + density of the lithospheric mantle 
then
echo "Temperature moho + density of the lithospheric mantle"
FILEPS=Tmoho_density.ps
rm -f $FILEPS

awk '{if($1!="#"){print $1,$2,$7-273 } }' $fileres > Tmoho.tmp
pstext titol.tmp -R$regio_vel -Y14.7 -X1.2 -JX10/5.5 -N -K -V > $FILEPS
psbasemap -Y-11 -X1 -R$regio_vel -Jx$scale_1Fig -Bnsew -V -K -O >> $FILEPS
xyz2grd Tmoho.tmp -R -GTmoho_grd.tmp -I$x1/$y1
makecpt -Cjet -T360/580/20 > file_palette_cpt.tmp 
#grd2cpt Tmoho_grd.tmp > file_palette_cpt.tmp
grdimage Tmoho_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
#grdview Tmoho_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -Qi50 -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D6/-1/14/.3h -B:" T@-moho@-(C)": -O -K -V  >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W15/100/255/100t35_35_0_0:5 -V >> $FILEPS
awk '{if($1!="#"){print $1,$2,$5 } }' $fileres > file_xycrust.tmp		## crust
xyz2grd file_xycrust.tmp -R$regio_vel -Gfile_crust_grd.tmp -I$x1/$y1
contour_text="T@-moho@-(C), contours crustal thickness (km)"
grdcontour file_crust_grd.tmp -Jx -R -B$Bx_tics:" $contour_text ":/$By_tics:"y (km)":enWS -C5 -A10f8 -G8/8 -W1/0 -O -K -V >> $FILEPS
rm -f Tmoho*.tmp

awk '{if($1!="#") {print $1,$2,$5 } }' $file_Pav$ext_res > file_xy.tmp
psbasemap -X13.5 -R$regio_vel -Jx$scale_1Fig -Bnsew  -V -K -O >> $FILEPS
xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
grd2cpt file_grd.tmp > file_palette_cpt.tmp
#makecpt -Chaxby -T3241/3249/0.25  > file_palette_cpt.tmp
grdimage file_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D11/7/16/.3 -B:" @~r@~@-m@- [kg/m@+3@+]": -O -K -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/100/0/0t35_35_0_0:5 -V >> $FILEPS
#grdcontour file_grd.tmp -C0.01 -A0.02 -G2/8 -O -Bnsew -Jx -R -V >> $FILEPS
awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xycont.tmp  		## elevation
contour_text=" contours elevation (km)" 					##  elevation
xyz2grd file_xycont.tmp -R -Gfile_crust_grd.tmp -I$x1/$y1 			## 
grdcontour file_crust_grd.tmp -R -C0.5 -A1f8 -G3/8 -W4/100 -Jx -B$Bx_tics:" $contour_text ":/$By_tics:"  ":nSew -O -V >> $FILEPS

fi


if [ $selec -eq 7 ]
then
###########################################################################
############  PRODUCCIO DE CALOR SUPERFICIAL ##############################
echo PRODUCCIO DE CALOR SUPERFICIAL
FILEPS=HSUP.ps
rm -f $FILEPS
fileH=PROHEAT

awk '{print ($1,$2,$3*1e6) }' $fileH > file_Hs.tmp
pstext titol.tmp -R$regio_vel -Y12 -JX10/8 -N -K -V > $FILEPS
psbasemap -Y-7 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:" H@-s@- \
	 (@~m@~W/m@+3@+)":/$By_tics:"y (km)":SeWn -V -K -O >> $FILEPS
xyz2grd file_Hs.tmp -R -Gfile_grd.tmp -I$x1/$y1
#############   Paleta Blanc i Negre  ############
cat <<END> file_palette_cpt.tmp
0	10	10	10 	 0.5	20	20	20 
0.5	55	55	55	 1	55	55	55  
1	90	90	90	 1.5	90	90	90
1.5	120	120	120	 2	120	120	120
2	160	160	160	 2.5	160	160	160
2.5	200	200	200	 3	200	200	200
3	250	250	250	 4	255	255	255
END
###################################################
#grdsample file_grd.tmp -Gfile_grd_sample.tmp -N101/101 -V 
file_grd=file_grd.tmp
grdview $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -Qi50 -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D16/6/10/.3 -B:." ": -O -K -V  >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W15/100/255/100t35_35_0_0:5 -V >> $FILEPS
grdcontour $file_grd -C0.5 -A1 -G2/8 -O -Jx -R -V >> $FILEPS

rm -f Tmoho*.tmp file_*.tmp
fi

if [ $selec -eq 8 ]		### Bodies + Crustal thickness 
then
FILEPS=Bodies_visco.ps
pstext titol.tmp -N -R$regio_vel -Y14.5 -JX10/6 -N -K -V > $FILEPS
awk '{if($1!="#") {print ($1,$2,$5)} }' $fileres > file_xyz.tmp
##### -Y0 -Jx.002 /  -Y-0.5 -Jx.0032 ##### 
psbasemap -Y-12 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:"contours crustal thickness [km] \
	":/$By_tics:" ":EnWS -O -K -V >> $FILEPS
#psbasemap -Y-13.5 -G255 -R$regio_vel -Jx$scale_1Fig -Bnsew -O -K -V >> $FILEPS
xyz2grd file_xyz.tmp -R -Gfile_grd.tmp -I$x1/$y1
#############   Paleta color  ############
cat <<END> file_palette_cpt.tmp
25	64	0	128	30	64	0	128
30	0	0	255	32	200	200	255
32	200	255	200	34	0	200	0
34	0	200	0	36	255	255	0
36	255	255	0	38	255	170	0
38	255	170	0	40	255	50	0
40	255	50	0	50	255	0	0
END
###########################
#grdsample file_grd.tmp -Gfile_grd_sample.tmp -N101/101 -V 
file_grd=file_grd.tmp
grd2cpt $file_grd > file_palette_cpt.tmp
#grdview $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -Qi50 -V >> $FILEPS
#psscale -Cfile_palette_cpt.tmp -L -D22/6/10/.3 -B:." ": -O -K -V >> $FILEPS
psxy $fcorba1 -R -Jx -M -L -W8/140/0/0 -G255/200/200 -O -K -V >> $FILEPS
psxy $fcorba1 -R -Jx -Sc0.2 -G100/100/255 -W3/0 -N -M -O -K -V >> $FILEPS
#psxy $fcorba2 -R -Jx -Sc0.3 -G140/0/0 -W3/0 -N -O -K -V >> $FILEPS
echo "  "
echo "What dots do you want to plot?  1- All Nodes   2- Nodes where the lithosphere has been removed "
read Pdots
if [ $Pdots -eq 1 ]
then
   psxy file_xyz.tmp -R -Jx -Sc0.05 -G0/0/0 -W3/0 -N -O -K -V >> $FILEPS		## Plot Nodes
else
   #awk '{if($3==":ConvectiveRemoval:") {print $1*x1,$2*y1 }}' x1=$x1 y1=$y1 $f_screen > file_xy_CR.tmp
   awk '{if($1=="ConvectiveRemoval:") {print $2,$3 }}' $f_screen > file_xy_CR.tmp
   wc file_xy_CR.tmp | read nCR b c d
   psxy file_xy_CR.tmp -R -Jx -Sc0.1 -G0/150/0 -B:"Lithosphere removal in $nCR points":N -N -O -K -V >> $FILEPS		## Points where lithosphere removed
fi
grdcontour $file_grd -C5 -A10f8 -G30/8 -W1/100 -Bg50 -O -Jx -R -V >> $FILEPS
rm -f file_*.tmp

fi

if [ $selec -eq 9 ]
then
###########################################################################
################  GRUIX SEDIMENTS  ###################################
echo GRUIX DELS SEDIMENTS
FILEPS=SEDIMENT.ps
pstext titol.tmp -N -R$regio_vel -Y14 -X4 -JX10/7 -N -K -V > $FILEPS
awk '{if($1!="#") {print $1,$2,$4 }}' $fileres > file_xyz.tmp
##### -Y0 -Jx.002 /  -Y-0.5 -Jx.0032 ##### 
psbasemap -Y-9 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:"x (km)":/$By_tics:"y (km)":WESn -V -K -O >> $FILEPS
xyz2grd file_xyz.tmp -R -Gfile_grd.tmp -I$x1/$y1
#grd2cpt file_grd.tmp > file_palette_cpt.tmp
cat <<END> file_palette_cpt.tmp
0	255	255	255	0.2	0	170	255
0.2	0	255	199	0.5	0	255	199
0.5	0	255	56	0.8	0	255	56
0.8	227	255	0	1.0	227	255	0
1.0	240	200	0	1.5	240	200	0
1.5	255	142	0	2.0	255	142	0
2.0	255	0	0	4.0	255	0	0
4.0	170	0	0	10.0	170	0	0
10.0	70	0	0	20.0	70	0	0
END
#grdsample file_grd.tmp -Gfile_grd_sample.tmp -N101/101 -V 
file_grd=file_grd.tmp
grdview $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -Qi50 -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D6.5/-2/13/.3h -B:" sediment thickness (km)": -O -K -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Bnsew -Jx -W15/100/100/100t35_35_0_0:5 -V >> $FILEPS

#   Px0=300    # profile
#   Py0=800
#   Pxf=550
#   Pyf=350
#psxy -R -Jx -W5/0/0/0 -N -O -K -V <<END>> $FILEPS
#$Px0 $Py0 
#$Pxf $Pyf
#END


grdcontour $file_grd -C0.5 -A1 -G2.5/8 -O -Jx -R -V >> $FILEPS
rm -f crust_*.tmp
rm -f file_*.tmp

fi


if [ $selec -eq 10 ]		###   CROSS SECTION  ##################
then
timestep=$ext_res
FILEPS=CROSS.ps
Tcolor=1	# Tcolor=1 -> Color,	Tcolor=2 -> Black and white

regio_vel=0/$x2vel/0/$y2vel
awk '{printf "%.3f ", x2vel/2}' x2vel=$x2vel $f_limits | read xvelmig
awk '{printf "%.3f ", y2vel/2}' y2vel=$y2vel $f_limits | read yvelmig

echo 'choose:'
echo '1: N-S Profile '
echo '2: W-E Profile'
echo '3: Oblique Profile'
read TProfile
## N-S Profile
if [ $TProfile -eq 1 ]		## N-S Profile
then
    Px0=2300	# $xvelmig
    Py0=0		#800	#0
    Pxf=2300	# $xvelmig
    Pyf=4000	#$y2vel		#5500	#$y2vel
fi
if [ $TProfile -eq 2 ]		## W-E Profile
then
    Px0=100	#0
    Py0=660	#$yvelmig
    Pxf=750	#$x2vel
    Pyf=$y2vel	#$yvelmig
fi
if [ $TProfile -eq 3 ]		## Oblique Profile
then
    Px0=4000	#0	# Atlas	
    Py0=1200	#600	# Atlas	
    Pxf=2800	#460	# Atlas	
    Pyf=2400	#130	# Atlas	
    #Px0=0	# Rif	
    #Py0=400	# Rif	
    #Pxf=600	# Rif	
    #Pyf=$y2vel	# Rif	
fi

awk '{print sqrt((Pxf-Px0)*(Pxf-Px0)+(Pyf-Py0)*(Pyf-Py0)) }' \
	Px0=$Px0 Pxf=$Pxf Py0=$Py0 Pyf=$Pyf $f_limits | read Distance
echo "Section from ($Px0,$Py0) to ($Pxf,$Pyf)"
echo Section of $Distance km

zmin=-8
zmax=240
zmax_Surf=3
R_cross=0/$Distance/$zmin/$zmax
R_crossSurf=0/$Distance/$zmin/$zmax_Surf

project -C$Px0/$Py0 -E$Pxf/$Pyf -N -G$y1 -M -V > fileprojy.tmp

pstext titol.tmp -P -N -R$regio_vel -X2 -Y24.3 -JX10/5 -N -K -V > $FILEPS

x_scale=17
y_scale=2.2
###   Surface heat flow
#   xc1=40
#   xc2=120
#   R_cross_1=0/$Distance/$xc1/$xc2
#   psbasemap -Y-0.5 -R$R_cross_1 -JX$x_scale/$y_scale -Ba100f50:" horizontal distance (km) ":/a20f5g40:"  \
#	Q@-s@- (mW/m@+2@+)":WsN -V -K -O >> $FILEPS					# Surface Heat Flow
#   awk '{if($1!="#"){print ($1,$2,$9)} }' $fileres > file_xy.tmp   			# Surface Heat Flow
###   vertical strain rate
   xc1=-11
   xc2=11
   R_cross_1=0/$Distance/$xc1/$xc2
   psbasemap -Y0 -R$R_cross_1 -JX$x_scale/$y_scale -B$Bx_tics:" $DIR_main ":/a5f2.5:"  \
	@~e@~@+.@+@-eff@- (10@+-16@+ s@+-1@+)":WsN -V -K -O >> $FILEPS					
awk '{if($1!="#"){print $1,$2,$3*1e16} }' $file_ep_uhuru$ext_res > file_xy.tmp		# effective strain rate
### 
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_cross_1 -JX$x_scale/$y_scale -W6/0/0/150 -K -O -V >> $FILEPS
rm -f file_*.tmp
pstext -R$R_cross_1 -JX -G200/0/0 -O -K -N -V <<END>> $FILEPS
0	$xc1	15 0 1 0 A
$Distance  $xc1	15 0 1 3 B
END
## viscosity
R_cross_2=0/$Distance/20.9/25.1	#25.1
   awk '{if($1!="#"){print $1,$2,log($8)/log(10) }}' $fileres > file_xy.tmp		#   viscosity
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_cross_2 -Ba1f0.5:"      log(@~h@~@-eff@- (Pa.s))":E -JX$x_scale/$y_scale -W6/200/0/0 -K -O -V >> $FILEPS
rm -f file_*.tmp

## vertical strain rate
R_cross_2=0/$Distance/-5.5/5.5
psbasemap -Y-2.5 -R$R_cross_2 -JX$x_scale/$y_scale -B$Bx_tics/a2f0.5g50:"@~e@~@+.@+@-zz@- (10@+-16@+ s@+-1@+)":Wsn -V -K -O >> $FILEPS
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres > file_xy.tmp			#   vertical strain rate
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_cross_2 -JX -W6/0/0/150 -K -O -V >> $FILEPS
rm -f file_*.tmp
## Depth average vertical stress
#R_cross_2=0/$Distance/-75/13								# Depth average vertical stress
#   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 $file_Pav$ext_res | read P0_aver
#   awk '{if($1!="#"){print $1,$2,($3-P0_aver)/1e6 }}' P0_aver=$P0_aver $file_Pav$ext_res > file_xy.tmp		
## Gravitational Potential Energy - GPE

R_cross_2=0/$Distance/-3/6							# GPE
   awk '{if($1!="#"){print $1,$2,$3} }' $file_Pav$ext_res > file_xy_av-sigmazz.tmp	# (Pa)
   awk '{if(NR==1){print $6} }' $file_Pav$ext_res | read ZASTH			# (km)
   awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xy_elevation.tmp	# elevation(km)
   sort -k 2,2n -k 1,1n -o file_sigma.tmp file_xy_av-sigmazz.tmp
   sort -k 2,2n -k 1,1n -o file_elevation.tmp file_xy_elevation.tmp
   paste file_sigma.tmp file_elevation.tmp > file_xy_sig_xy_ele.tmp
   awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_sig_xy_ele.tmp 
   awk '{print $1,$2,$3*(ZASTH+$6)*1e3}' ZASTH=$ZASTH file_xy_sig_xy_ele.tmp > file_xy_Zval.tmp
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 file_xy_Zval.tmp | read Valor0
   awk '{if($1!="#"){print $1,$2,($3-Valor0)/1e12}}' Valor0=$Valor0 file_xy_Zval.tmp > file_xy.tmp		

xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   #psxy file_2.tmp -R$R_cross_2 -B$Bx_tics/a20f10:" @~Ds@~@-zz@- [MPa] red":E -JX$x_scale/-$y_scale -W6/200/0/0 -K -O -V >> $FILEPS
   psxy file_2.tmp -R$R_cross_2 -B$Bx_tics/a2f1g100:"@~D@~GPE[10@+12@+ N/m] red":E -JX$x_scale/$y_scale -W6/200/0/0 -K -O -V >> $FILEPS
rm -f file_*.tmp

## Velocity, East and North
vel_label=-10/55
R_cross_2=0/$Distance/$vel_label
psbasemap -Y-2.5 -R$R_cross_2 -JX$x_scale/$y_scale -B$Bx_tics/a10f2:"V@-n@- (mm/yr)":Wsn -V -K -O >> $FILEPS
   awk '{print $1,$2,$4}' $fvelocity > file_xy.tmp			## North Velocity
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_cross_2 -JX -W6/0/0/150 -K -O -V >> $FILEPS
rm -f file_*.tmp
R_cross_2=0/$Distance/$vel_label	
   awk '{print $1,$2,$3}' $fvelocity > file_xy.tmp			## East Velocity
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_cross_2 -B$Bx_tics/a10f2:"V@-e@- (mm/yr)":E -JX$x_scale/$y_scale -W6/200/0/0 -K -O -V >> $FILEPS
rm -f file_*.tmp


####   surface structure
psbasemap -Y-3.9 -R$R_crossSurf -JX$x_scale/-3.5 -B$Bx_tics:" horizontal distance (km) ":/a1f0.5g1:" elevation (km) \
	":WsEn -V -K -O >> $FILEPS

psxy -R$R_crossSurf -G0/0/200 -JX -K -O -V <<END>> $FILEPS   			# Sea
0 0
$Distance 0
$Distance $zmax_Surf
0 $zmax_Surf
END

awk '{if($1!="#"){print $1,$2,$3 } }' $fileres > filetopo.tmp			# elevation (surface)
text_e="e [km]"
if [ -f $file_tisc$ext_res ]							# elevation from tisc
then
   echo "  -------------------  Do you want to plot the elevation resulting from tisc? 1-Yes ------ "
   read eflex
if [ $eflex -eq 1 ]
then   
   awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_tisc$ext_res > file_topo.tmp
   sort -k 2,2n -k 1,1n -o filetopo.tmp file_topo.tmp
   #name=res_tisc
   #awk '{if(NR>2){print $1,$2,$6/1e3 }}' $name$timestep.xyw > filetopo.tmp
   text_e="e - tisc [km]"
fi

fi
awk '{print $1,$2,-$3 }' filetopo.tmp > file_xy.tmp
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   #psxy file_2.tmp -R$R_crossSurf -JX -W10/0/250/0 -K -O -V >> $FILEPS
   #awk '{print($1,"       ",-$2)}' file_2.tmp > elevation.cross.txt
   wc file_2.tmp | read nlines b c d
     cat file_2.tmp > file_poligon.tmp
     awk '{if(NR==nlines){print $1,zmax}}' \
         nlines=$nlines zmax=$zmax file_2.tmp >> file_poligon.tmp
     awk '{if(NR==1){print $1,zmax}}' zmax=$zmax file_2.tmp >> file_poligon.tmp
   psxy file_poligon.tmp -R$R_crossSurf -G230/230/0 -W1/0 -JX -O -K -V >> $FILEPS   # sediments
rm -f file_*.tmp

#awk '{if($1!="#"){print $1,$2,$4-$3 }}' $fileres > file_xy.tmp   		# base sediments
awk '{if($1!="#"){print $1,$2,$4 }}' $fileres > file_xy_sed1.tmp   			# base sediments
sort -k 2,2n -k 1,1n -o file_xy_sed.tmp file_xy_sed1.tmp
paste file_xy_sed.tmp filetopo.tmp > file_sed_topo.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_sed_topo.tmp 
awk '{print $1,$2,$3-$6 }' file_sed_topo.tmp > file_xy.tmp
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
#   psxy file_2.tmp -R$R_crossSurf -JX -W2/0/200/0 -K -O -V >> $FILEPS
   wc file_2.tmp | read nlines b c d
     cat file_2.tmp > file_poligon.tmp
     awk '{if(NR==nlines){print $1,zmax}}' \
         nlines=$nlines zmax=$zmax file_2.tmp >> file_poligon.tmp
     awk '{if(NR==1){print $1,zmax}}' zmax=$zmax file_2.tmp >> file_poligon.tmp
   psxy file_poligon.tmp -R$R_crossSurf -G150/75/30 -W1/0 -JX -K -O -V >> $FILEPS  	# crust
rm -f file_*.tmp
psxy -R$R_crossSurf -W1/0/0/200 -JX -K -O -V <<END>> $FILEPS   			# Sea
0 0
$Distance 0
END
####


## Lithospheric structure
psbasemap -Y-7.3 -R$R_cross -JX$x_scale/-6.8 -B$Bx_tics:" horizontal distance (km) ":/a20f5:" depth (km) \
	":WSEn -V -K -O >> $FILEPS

psxy -R$R_cross -G0/0/200 -JX -K -O -V <<END>> $FILEPS   			# Sea
0 0
$Distance 0
$Distance $zmax
0 $zmax
END

#awk '{if($1!="#"){print $1,$2,-$3 }}' $fileres > file_xy.tmp				# elevation (surface)
awk '{print $1,$2,-$3 }' filetopo.tmp > file_xy.tmp				# elevation (surface)
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
#cp file_grd.tmp elevation_tmp.grd
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
#   psxy file_2.tmp -R$R_cross -JX -W2/0/0/200 -K -O -V >> $FILEPS
   wc file_2.tmp | read nlines b c d
     cat file_2.tmp > file_poligon.tmp
     awk '{if(NR==nlines){print $1,zmax}}' \
         nlines=$nlines zmax=$zmax file_2.tmp >> file_poligon.tmp
     awk '{if(NR==1){print $1,zmax}}' zmax=$zmax file_2.tmp >> file_poligon.tmp
   psxy file_poligon.tmp -R$R_cross -G230/230/0 -W1/0 -JX -O -K -V >> $FILEPS   # sediments
rm -f file_*.tmp

#awk '{if($1!="#"){print $1,$2,$4-$3 }}' $fileres > file_xy_sed1.tmp   			# base sediments
awk '{if($1!="#"){print $1,$2,$4 }}' $fileres > file_xy_sed1.tmp   			# base sediments
sort -k 2,2n -k 1,1n -o file_xy_sed.tmp file_xy_sed1.tmp
paste file_xy_sed.tmp filetopo.tmp > file_sed_topo.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_sed_topo.tmp 
awk '{print $1,$2,$3-$6 }' file_sed_topo.tmp > file_xy.tmp
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
#   psxy file_2.tmp -R$R_cross -JX -W2/0/200/0 -K -O -V >> $FILEPS
   wc file_2.tmp | read nlines b c d
     cat file_2.tmp > file_poligon.tmp
     awk '{if(NR==nlines){print $1,zmax}}' \
         nlines=$nlines zmax=$zmax file_2.tmp >> file_poligon.tmp
     awk '{if(NR==1){print $1,zmax}}' zmax=$zmax file_2.tmp >> file_poligon.tmp
   psxy file_poligon.tmp -R$R_cross -G150/75/30 -W1/0 -JX -K -O -V >> $FILEPS  	# crust
rm -f file_*.tmp

#awk '{if($1!="#"){print $1,$2,($4-$3)+$5} }' $fileres > file_xy.tmp   			# base crust
awk '{if($1!="#"){print $1,$2,$4+$5 }}' $fileres > file_xy_c1.tmp   			# base sediments
sort -k 2,2n -k 1,1n -o file_xy_c.tmp file_xy_c1.tmp
paste file_xy_c.tmp filetopo.tmp > file_c_topo.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_c_topo.tmp 
awk '{print $1,$2,$3-$6 }' file_c_topo.tmp > file_xy.tmp
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
#cp file_grd.tmp moho_tmp.grd
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
#   psxy file_2.tmp -R$R_cross -JX -W2/100/100/0 -K -O -V >> $FILEPS
   wc file_2.tmp | read nlines b c d
     cat file_2.tmp > file_poligon.tmp
     awk '{if(NR==nlines){print $1,zmax}}' \
         nlines=$nlines zmax=$zmax file_2.tmp >> file_poligon.tmp
     awk '{if(NR==1){print $1,zmax}}' zmax=$zmax file_2.tmp >> file_poligon.tmp
   psxy file_poligon.tmp -R$R_cross -G240/140/90 -W1/0 -JX -K -O -V >> $FILEPS   # lithospheric mantle
rm -f file_*.tmp

awk '{if($1!="#"){print ($1,$2,$6-$3) }}' $fileres > file_xy.tmp   			# base lithosphere
awk '{if($1!="#"){print $1,$2,$6 }}' $fileres > file_xy_l1.tmp   			# base sediments
sort -k 2,2n -k 1,1n -o file_xy_l.tmp file_xy_l1.tmp
paste file_xy_l.tmp filetopo.tmp > file_l_topo.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_l_topo.tmp 
awk '{print $1,$2,$3-$6 }' file_l_topo.tmp > file_xy.tmp
   xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
#cp file_grd.tmp Ldepth_tmp.grd
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
#   psxy file_2.tmp -R$R_cross -JX -W2/250/0/0 -K -O -V >> $FILEPS
   wc file_2.tmp | read nlines b c d
     cat file_2.tmp > file_poligon.tmp
     awk '{if(NR==nlines){print $1,zmax}}' \
         nlines=$nlines zmax=$zmax file_2.tmp >> file_poligon.tmp
     awk '{if(NR==1){print $1,zmax}}' zmax=$zmax file_2.tmp >> file_poligon.tmp
   psxy file_poligon.tmp -R$R_cross -G255/50/50 -W1/0 -JX -K -O -V >> $FILEPS    #asthenosphere

####		Conductivity transition (lithosphere - asthenosphere)
#   sort -k 2,2n -k 1,1n -o file_xy_GL_o.tmp $f_resGLit				
#   awk '{if($1!="#"){print $1,$2,$3 }}' $fileres > file_elev.tmp		# elevation (surface)
#   sort -k 2,2n -k 1,1n -o file_elev_o.tmp file_elev.tmp			
#   paste file_xy_GL_o.tmp  file_elev_o.tmp > file_paste.tmp
#   awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_paste.tmp 
#   awk '{print $1,$2,$3-$6 }' file_paste.tmp > file_xy.tmp   			# base lithosphere (GLit)
#   xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
#   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
#   awk '{print $3,$4 }' file_1.tmp > file_2.tmp
#   psxy file_2.tmp -R$R_cross -W10/0 -JX -O -K -V >> $FILEPS   
####

ztext=$(($zmin-3)) 
pstext -R -JX -O -K -N -V <<END>> $FILEPS
#15 $zmin 8 0 0 0 Cross section from A=($Px0, $Py0) to B=($Pxf, $Pyf)
15 $ztext 10 0 0 0 Cross section from A=($Px0, $Py0) to B=($Pxf, $Pyf)
END

#awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xy.tmp
xyz2grd filetopo.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1


if [ $Tcolor -eq 1 ]
then
   cp filePalette_cpt_color2.tmp file_palette_cpt.tmp
else
   cp filePalette_cpt_BW.tmp file_palette_cpt.tmp
fi 

scale=$scale_4Fig		# $(($scale_4Fig/1.3))
psbasemap -Y-7.1 -R$regio_vel -Jx$scale -Bnsew -O -K -V >> $FILEPS
grdimage file_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D13/2.9/5.8/.25 -B:"km ": -O -K -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W3/100 -N -V >> $FILEPS

timestep=$ext_res
name=res_tisc
tmp=file_
col_river=0/0/255
size_lake_squares=0.2
#if (-r $name.xyw) then 
#if (-r $name.RIV) awk '{if (substr($0,1,1)!=">" && substr($0,1,1)!="#") print $1/1000,$2/1000; else print $0}' \
#	$name.RIV | psxy -JX -R -M -W4/70/0/0 -O -K >> $FILEPS  
awk '{if ($3>25 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W1/$col_river -H2 -O -K >> $FILEPS 
awk '{if ($3>200 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W4/$col_river -H2 -O -K >> $FILEPS 
awk '{if ($3>1000 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W8/$col_river -H2 -O -K >> $FILEPS 
#awk '{if ($5=="L") print $1, $2}' $name$timestep.xyw | \
#	psxy -Jx -R -Ss$size_lake_squares -W1/$col_river -G$col_river -O -K >> $FILEPS 
#awk '{if ($5=="E") print $1, $2}' $name$timestep.xyw | \
#	psxy -Jx -R -St$size_lake_squares -W1/$col_river -G$col_river -O -K >> $FILEPS 
awk -vsea_level=$sea_level '{if (($5=="L" || $5=="E")) print $1,$2,1; else print $1,$2,0}' $name$timestep.xyw | \
		xyz2grd -G$tmp.lakes.grd.tmp -I$x1/$y1 -R -H2
cat - <<END>  $tmp.lakes_cpt.tmp
	0	0	0	0	.49	0	0	0
	.49	0	140	255	1	0	120	255
END
grdcontour $tmp.lakes.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -W3/$col_river -O -K >> $FILEPS
grdclip $tmp.lakes.grd.tmp -G$tmp.lakes.grd.tmp -Sb0.5/NaN
grdimage $tmp.lakes.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -Q -O -K >> $FILEPS

psxy -R -Jx -W9/0/0/0 -N -B$Bx_tics_4/$By_tics_4:" ":WneS -O -K -V <<END>> $FILEPS
$Px0 $Py0 
$Pxf $Pyf
END
pstext -R -Jx -G200/0/0 -O -N -V <<END>> $FILEPS
$Px0 $Py0  13 0 1 0 A
$Pxf $Pyf  13 0 1 0 B
END

rm -f file_*.tmp

fi

if [ $selec -eq 11 ]		 #######  elevation+drainage, sedimentation+erosion
then
FILEPS=Rain-Sedim.ps
rm -f $FILEPS
Tcolor=1	# Tcolor=1 -> Color,	Tcolor=2 -> Black and white

awk '{if($1!="#"){print ($1,$2,$3) }}' $fileres > file_xy.tmp	## elevation
text_e="elevation (km)"
if [ -f $file_tisc$ext_res ]							# elevation from tisc
then
   echo "  -------------------  Do you want to plot the elevation resulting from tisc? 1-Yes ------ "
   read eflex
if [ $eflex -eq 1 ]
then   
   awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_tisc$ext_res > file_topo.tmp
   sort -k 2,2n -k 1,1n -o file_xy.tmp file_topo.tmp
   text_e="elevation - tisc (km)"
fi
fi

echo " Use the measured topography from the grid" 
echo $file_topodata
echo " 1-Yes "
Var_topodata=2
#read Var_topodata

xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
cp file_grd.tmp filetopo_grd.tmp
if [ $Tcolor -eq 1 ]
then
   cp filePalette_cpt_color2-topo.tmp file_palette_cpt.tmp
else
   cp filePalette_cpt_BW.tmp file_palette_cpt.tmp
fi 
#pstext titol.tmp -P -R -X1 -Y25 -Jx$scale_4Fig -N -K -V > $FILEPS
#pstext -P -R -X1 -Y25 -Jx$scale_4Fig -N -K -V <<END> $FILEPS
#$x2vel $y2vel 16 0 4 2  $TIME_My My
#END
#psbasemap -Y16.8 -X2 -P -R$regio_vel -Jx$scale_2Fig -B$Bx_tics:"$DIR_main":/$By_tics:" ":WNes -K -V > $FILEPS
psbasemap -Y6 -X3 -R$regio_vel -Jx$scale_2Fig -B$Bx_tics:"$DIR_main":/$By_tics:" ":WNes -K -V > $FILEPS
xtext=$(($x2vel/2))
ytext=$(($y2vel+$y2vel/10))
#ytext=$(($y2vel+6*$y1))
pstext -R -Jx -N -K -O -V <<END>> $FILEPS
$xtext $ytext 16 0 4 1  $TIME_My My
END
file_grd=file_grd.tmp
grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
#psxy $fcorba1 -R -O -K -M -L -Jx -W2/0/0/0t35_5_0_0:5 -V >> $FILEPS
#psxy $fcorba2 -R -O -K -Jx -Sc0.2 -G255 -W1/0 -N -V >> $FILEPS

if [ $Var_topodata -eq 1 ]
then   
   grdcontour $file_topodata -Bnsew -W10/0 -C5 -Jx -R -O -K -V >> $FILEPS
   grdcontour $file_topodata -Bnsew -W3/0 -C0.5 -A1f9 -G4/8 -Jx -R -O -K -V >> $FILEPS
else
  # grdcontour $file_grd -Bnsew -C0.5 -A1f6 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
fi
psscale -Cfile_palette_cpt.tmp -L -D5.5/-0.25/10/.2h -B:" $text_e ": -O -K -V >> $FILEPS

rm -f file_*.tmp

#echo "Drainage: Which time step? (que sigui el mateix que el fitxer de resultats)"  # drainage
#read timestep 
timestep=$ext_res
name=res_tisc
tmp=file_
col_river=0/0/155

#size of the lakes depending on domain area:
area=$(($x2vel*$y2vel))
#disch1=$(($area/90000))
disch1=200

if [ -f $name$timestep.xyw ] 
then
awk '{if ($3>100 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W1/$col_river -H2 -O -K >> $FILEPS 
awk '{if ($3>500 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W4/$col_river -H2 -O -K >> $FILEPS 
awk '{if ($3>3000 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W8/$col_river -H2 -O -K >> $FILEPS 
#Llacs
cat - <<END>  $tmp.lakes_cpt.tmp
	0	0	0	0	.49	0	0	0
	.49	0	40	255	1	0	40	255
END
awk -v sl=$sea_level '(substr($0,1,1)!="#"){if (substr($0,1,1)==">") disch=$11; else{if (disch>dl && ($3=="L" || $3=="E")) print $1,$2,1; else print $1,$2,0}}' dl=$disch1 $name$timestep.lak | \
	xyz2grd -G$tmp.lakes.grd.tmp -I$x1/$y1 -R -H2 -N0
grdclip $tmp.lakes.grd.tmp -G$tmp.lakes2.grd.tmp -Sb0.5/NaN
grdimage $tmp.lakes2.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -Q -O -K >> $FILEPS
grdcontour $tmp.lakes.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -W2/$col_river -O -K >> $FILEPS

#Llacs endorheics
cat - <<END>  $tmp.lakes_cpt.tmp
	0	0	0	0	.49	0	0	0
	.49	20	20	255	1	20	20	255
END
awk -v sl=$sea_level '(substr($0,1,1)!="#"){if (substr($0,1,1)==">") {disch=$11; if ($9!=0) disch=0} else{if (disch>dl && ($3=="L" || $3=="E")) print $1,$2,1; else print $1,$2,0}}' dl=$disch1 $name$timestep.lak | \
	xyz2grd -G$tmp.lakesendorh.grd.tmp -I$x1/$y1 -R -H2 -N0
grdclip $tmp.lakesendorh.grd.tmp -G$tmp.lakes2.grd.tmp -Sb0.5/NaN
grdimage $tmp.lakes2.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -Q -O -K >> $FILEPS
grdcontour $tmp.lakesendorh.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -W2/$col_river -O -K >> $FILEPS

####  Plotting rain - Precipitation
rain_min=0.4
echo rain_min $rain_min
awk '{print ($1,$2,$10/1e3)}' $name$timestep.xyw | xyz2grd -Gfile_grd.tmp -I$x1/$y1 -R -H2
#makecpt -Ccool -T$rain_min/9/1 > file_cpt.tmp
cat <<END> file_cpt.tmp
$rain_min  255  70  70			2  255  0  0
2  255  0 0			5  180  0  0
5  180  0  0			8  20  0  0
B   255  150  150
F   20  0  0
END

#$rain_min  255  190  255	2  255  110  200		## Pink palette
#2  255  110  200		4  245  60  130
#4  245  60  130			6  250  0  0
#6  250  0  0			8  50  0  0
#B   255  190  255
#F   50  0  0
#END

grdclip file_grd.tmp -Gfile_grd.tmp -Sb$rain_min/NaN
grdimage file_grd.tmp -Cfile_cpt.tmp -Bnsew -J -R -Q -K -O >> $FILEPS
psscale -Cfile_cpt.tmp -L -D5.5/-1.4/10/.2h -B:" rain (m/yr) ": -O -K -V >> $FILEPS

#dist_rose=112	#160
#y_rose1=$((350-$dist_rose))
#y_rose2=$((350+$dist_rose))
#emin_rose=300
#w_min=2		# caudal minim
#psxy -R -M -Jx -W8/120/120/120ta -O -K -V <<END>> $FILEPS
#0	$y_rose1
#700	$y_rose1
#>
#700	$y_rose2
#0	$y_rose2
#END

 ##awk '($2>=y_rose1 && $2<=y_rose2 && $3>w_min && $6>emin_rose){print $1,$2}' \
 ##	y_rose1=$y_rose1 y_rose2=$y_rose2 w_min=$w_min emin_rose=$emin_rose $name$timestep.xyw | 
 ##	psxy -R -Jx -Sc.05 -G150 -O -K -V >> $FILEPS
	
##awk '($2>=y_rose1 && $2<=y_rose2 && $3>w_min && $6>emin_rose)
##y_rose1=$y_rose1 y_rose2=$y_rose2 w_min=$w_min emin_rose=$emin_rose 
#awk '($5!="L" && !(($8-$2)==0 && ($7-$1)==0) && $3>1000){print $3, 90-atan2($8-$2,$7-$1)*180/3.1415927}' \
# 	$name$timestep.xyw | 
#	psrose -S2.5n -R0/1/0/360 -A45 -X12 -Y3 -Ba.25g.25/a45g45:."river orientation, e>$emin_rose, w>$w_min": -G220 -D -O -K >> $FILEPS


fi

##  total sedimentat i erosionat	##########  sediment thickness --------
awk '{	if($5=="Time") time=$7; \
	if($1=="noSed" && time<=TIME_My) {totaleros+=$3; totalsed+=$6} }
	END {printf "%.3e  %.3e", totaleros/9.81, totalsed/9.81}' \
	TIME_My=$TIME_My $f_screen | read totaleros totalsed

#awk '{if($1!="#"){print ($1,$2,$4) }}' $fileres > file_xy.tmp
#xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1

file_eros=res_erosion$timestep
min_sed=0.2	# Minimum sediment thickness
awk '{if($1!="#") {print $1/1e3,$2/1e3,-1*$3/1e3 } }' $file_eros > file_eros1.tmp	# km
sort -k 2,2n -k 1,1n -o file_eros.tmp file_eros1.tmp
wc file_eros.tmp | read row_eros a b c
awk '{if($1!="#") {printf " %11.3f %11.3f %10.4f\n ",$1,$2,$4 }}' $fileres > file_sediments2.tmp
awk '{if(NR<=row_eros) {print $0 }}' row_eros=$row_eros file_sediments2.tmp > file_sediments1.tmp
sort -k 2,2n -k 1,1n -o file_sediments.tmp file_sediments1.tmp
paste file_eros.tmp file_sediments.tmp > file_eros_sed.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_eros_sed.tmp 
awk '{if($6>min_sed) {print $1,$2,$6} else {print $1,$2,$3} }' \
	min_sed=$min_sed file_eros_sed.tmp > file_xy.tmp

xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1

#grd2cpt $file_grd > filecpt_Sediment-Erosio.tmp
#makecpt -Chaxby -T-2/2/0.2 > filecpt_Sediment-Erosio.tmp
#psbasemap -Y-14.2 -R -Jx$scale_2Fig -B$Bx_tics:"totaleros= $totaleros kg, totalsed= $totalsed kg":/$By_tics:" ":WNEs -K -O -V >> $FILEPS
psbasemap -X12 -R -Jx$scale_2Fig -B$Bx_tics:" Erosion and Sediment thicknesses":/$By_tics:" ":wNEs -K -O -V >> $FILEPS
file_grd=file_grd.tmp
#grdsample litosfera_grd.tmp -Gfile_grd_sample.tmp -N100/100 -V -R
#grdview $file_grd -Jx -R -O -K -Cfilecpt_Sediment-Erosio.tmp -Qi50 -V >> $FILEPS
grdimage $file_grd -Jx -R -O -K -Cfilecpt_Sediment-Erosio.tmp -V >> $FILEPS
psxy $fcorba1 -Bnsew -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
#grdcontour $file_grd -Bnsew -C1 -A2f6 -L0.2/5 -G4/8 -O -K -Jx -R -V >> $FILEPS
if [ $Var_topodata -eq 1 ]
then   
   grdcontour $file_topodata -Bnsew -W10/0 -C5 -Jx -R -O -K -V >> $FILEPS
fi
psscale -Cfilecpt_Sediment-Erosio.tmp -L -D5.5/-0.5/11.6/.2h -B:" erosion(-)/sediment(+) thickness (km) ": -O -V >> $FILEPS
#pstext -R -Jx -O -N -V <<END>> $FILEPS
#$xtit $ytitsota 12 0 4 2 thickness (km)
#$x2vel 120 11 0 4 1 '   ' totaleros= $totaleros kg
#$x2vel 50 11 0 4 1   '   ' totalsed= $totalsed kg
#END

rm -f file_*.tmp 

fi



if [ $selec -eq 12 ]	########  VELOCITIES TO THE BOUNDARIES  
then
echo 
FILEPS=velocity_Bound.ps
rm -f $FILEPS
vmin=0
pstext titol.tmp -R$regio_vel -Y15 -JX10/6 -N -K -V > $FILEPS
psbasemap -Y-12 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:"velocity  >  $vmin mm/any":/$By_tics:" \
	y (km)":EnWS -O -K -V >> $FILEPS
#psbasemap -Y-9 -R$regio_vel -Jx$scale_1Fig -Bnsew -O -K -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W10/0/0/0ta -V >> $FILEPS

awk '{if($3!=0 || $4!=0){print $1,$2,(180/3.1416)*atan2($4,$3),sqrt($3*$3+$4*$4)} \
	else {print $1,$2,0,0}}' $fvelocity > file_az_vel.tmp
	
awk '{if($1==0 || $1==x2vel || $2==0 || $2==y2vel){print ($1,$2,$3,$4/facvel)} }' \
	x2vel=$x2vel y2vel=$y2vel facvel=$facvel file_az_vel.tmp > file_velBC.tmp
cat <<END>> file_velBC.tmp
50 -160 0 2
END
tic_facvel=$(($facvel*2)) 

#psxy file_velBC.tmp -R -K -O -N -Jx -Sv.1/0.45/0.15n0.5 -G255/0/0 -W2/0 -V >> $FILEPS
psxy file_velBC.tmp -R -K -O -N -Jx -Sv.15/0.7/0.25n1 -G255/0/0 -W3/0 -V >> $FILEPS
#psxy $fcorba2 -R -O -K -Jx -Sc0.2 -G200 -W3/0 -N -V >> $FILEPS

pstext -R -Jx -O -N -V <<END>> $FILEPS
50 -130 11 0 9 0 $tic_facvel mm/year
END

rm -f file_*.tmp
fi



# Lithospheric mantle = ZISOTER - scrust - sediment

if [ $selec -eq 13 ]
then
#echo 'Do you want to aplied a Gaussian filter for the data ?  1-yes' 
#read Sifilter
Sifilter=5
   if [ $Sifilter -eq 1 ]
   then
	echo 'Gaussian wide in km?' 
	read Gaussian
   fi

echo 'elevation, crustal thick, lithospheric thick, surface heat flow'
FILEPS=esLQ.ps
Tcolor=1	# Tcolor=1 -> Color,	Tcolor=2 -> Black and white


##----------  FIRST - elevation ----------

awk '{if($1!="#"){print $1,$2,$3 }}' $fileres > file_xy.tmp

xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
cp file_grd.tmp filetopo_grd.tmp
if [ $Tcolor -eq 1 ]
then
   cp filePalette_cpt_color1.tmp file_palette_cpt.tmp
else
   cp filePalette_cpt_BW.tmp file_palette_cpt.tmp
fi 
#cat <<END> file_palette_cpt.tmp
#-1 10 10 10 0.5 10 10 10
#END
#makecpt -Chaxby -T0.5/5.3/0.4 >> file_palette_cpt.tmp 
#cat <<END>> file_palette_cpt.tmp
#5.3 120 120 120 8 120 120 120
#END

pstext titol.tmp -P -R$regio_vel -Y24.3 -JX10/4.5 -N -K -V > $FILEPS
#pstext titol.tmp -P -R -X1 -Y23.4 -Jx$scale_4Fig -N -K -V > $FILEPS
psbasemap -Y-4 -R -Jx$scale_4Fig -B$Bx_tics_4/$By_tics_4:" ":WNes -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi
#grd2cpt $file_grd > file_palette_cpt.tmp
grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
#pscoast -R -Jx -Di -A0/1 -W10 -K -O -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
#psxy $fcorba2 -R -O -K -Jx -Sc0.2 -G255 -W1/0 -N -V >> $FILEPS
grdcontour $file_grd -Bnsew -C0.3 -A0.6f6 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D13/2.7/5.5/.3 -B:" ": -O -K -V >> $FILEPS
pstext -R -Jx -Bnsew -O -K -N -V <<END>> $FILEPS
$xtit $ytit 12 0 4 2  elevation (km)
END

rm -f file_*.tmp


#-----  SECOND -  crustal thickness  -----
awk '{if($1!="#"){print ($1,$2,$5)} }' $fileres > file_xy.tmp
var_text="crustal thickness"
#awk '{if($1!="#"){print $1,$2,($4-$3)+$5} }' $fileres > file_xy.tmp
#var_text="moho depth"

xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
#cp file_grd.tmp crust_tmp.grd
if [ $Tcolor -eq 1 ]
then
cat <<END> file_palette_cpt.tmp
15	255	200	255	24	255	200	255
24	255	0	255	26	255	0	255
26	200	0	255	28	200	0	255
28	113	0	255	30	113	0	255
30	0	28	255	32	0	28	255
32	0	170	255	34	0	170	255
34	0	255	199	36	0	255	199
36	0	255	56	38	0	255	56
38	85	255	0	40	85	255	0
40	227	255	0	42	227	255	0
42	255	142	0	46	255	142	0
46	255	0	0	55	255	0	0
55	170	0	0	80	170	0	0
END
else
cat <<END> file_palette_cpt.tmp
10  	0       0       0       15	0       0       0
15 	28      28      28      20	28      28      28
20 	57      57      57      24	57      57      57
24 	85      85      85      28	85      85      85
28	113     113     113     32	113     113     113
32	142     142     142     34	142     142     142
34	170     170     170     36	170     170     170
36	200     200     200     38	200     200     200
38	227     227     227     40	227     227     227
40	255     255     255     100	255     255     255
END
fi
cat <<END> file_palette_cpt.tmp
15	255	200	255	34	255	200	255
34	255	0	255	38	255	0	255
38	200	0	255	42	200	0	255
42	113	0	255	46	113	0	255
46	0	28	255	50	0	28	255
50	0	170	255	54	0	170	255
54	0	255	199	58	0	255	199
58	0	255	56	62	85	255	0
62	227	255	0	66	227	255	0
66	255	142	0	70	255	142	0
70	255	0	0	74	255	0	0
74	170	0	0	80	170	0	0
END
#cat <<END> file_palette_cpt.tmp
#5 0 0 40	25 0 0 40
#25 0 0 80	30 0 0 80
#END
#makecpt -Cjet -T30/60/3 >> file_palette_cpt.tmp 
#cat <<END>> file_palette_cpt.tmp
#60 120 0 0 80 120 0 0
#END

psbasemap -Y-6.5 -R -Jx -B$Bx_tics_4/$By_tics_4:" ":Wnes -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi
#grdsample litosfera_grd.tmp -Gfile_grd_sample.tmp -N100/100 -V -R
#grd2cpt $file_grd > file_palette_cpt.tmp
#grdview $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -Qi50 -V >> $FILEPS
grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
#pscoast -R -Jx -Di -A0/1 -W10 -K -O -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
grdcontour $file_grd -Bnsew -C2 -A4f6 -G3/8 -O -K -Jx -R -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D13/2.6/5.5/.3 -B:"  ": -O -K -V >> $FILEPS
pstext -R -Jx -Bnsew -O -K -N -V <<END>> $FILEPS
$xtit $ytit 12 0 4 2 $var_text (km) 
END

rm -f file_*.tmp 

##----------  THIRD  - lithospheric thickness ----------
if [ $Tcolor -eq 1 ]
then
cat <<END> file_palette_cpt.tmp
10	255	200	255	50	255	200	255
50	255	0	255	60	255	0	255
60	113	0	255	70	113	0	255
70	0	28	255	80	0	28	255
80	0	170	255	90	0	170	255
90	0	255	199	100	0	255	199
100	0	255	56	110	0	255	56
110	85	255	0	120	85	255	0
120	227	255	0	130	227	255	0
130	255	142	0	150	255	142	0
150	255	0	0	200	255	0	0
END
else
cat <<END> file_palette_cpt.tmp
50  	0       0       0       60	0       0       0
60 	28      28      28      70	28      28      28
70 	57      57      57      80	57      57      57
80 	85      85      85      90	85      85      85
90	113     113     113     95	113     113     113
95	142     142     142     100	142     142     142
100	170     170     170     105	170     170     170
105	200     200     200     110	200     200     200
110	227     227     227     115	227     227     227
115	255     255     255     260	255     255     255
END
fi

#var_text="lithospheric mantle thickness"
#awk '{if($1!="#"){print $1,$2,$6-$4-$5 }}' $fileres > file_xy.tmp 
#cat <<END> file_palette_cpt.tmp
#0 120 0 0 20 120 0 0
#END
#makecpt -Cjet -I -T20/110/10 >> file_palette_cpt.tmp 
#cat <<END>> file_palette_cpt.tmp
#110 0 0 80 150 0 0 80
#END

var_text="lithospheric thickness"
awk '{if($1!="#"){print $1,$2,$6 }}' $fileres > file_xy.tmp   #lithospheric thickness
makecpt -Cjet -I -T60/250/20 > file_palette_cpt.tmp 
cat <<END>> file_palette_cpt.tmp
F   0  0  100
B   100  0  0
END

#var_text="lithospheric depth"
#awk '{if($1!="#"){print $1,$2,$6-$3 }}' $fileres > file_xy.tmp #lithospheric depth

xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
#cp file_grd.tmp Lithosphere_tmp.grd
psbasemap -R -Y-6.5 -Jx -B$Bx_tics_4/$By_tics_4:" ":Wnes -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi
#grdsample litosfera_grd.tmp -Gfile_grd_sample.tmp -N100/100 -V -R
#grd2cpt $file_grd > file_palette_cpt.tmp
#grd2cpt $file_grd -C300/0 -L60/160 -S60/360/20 -V > file_palette_cpt.tmp
#grdview $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -Qi50 -V >> $FILEPS
grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
#pscoast -R -Jx -Di -A0/1 -W10 -K -O -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
#grdcontour $file_grd -Bnsew -C2 -A4f6 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D13/2.6/5.5/.3 -B:"  ": -O -K -V >> $FILEPS
pstext -R -Jx -Bnsew -O -K -N -V <<END>> $FILEPS
$xtit $ytit  12 0 4 2 $var_text (km)
END

rm -f file_*.tmp

##----------  FOURTH  - surface heat flow ----------
if [ $Tcolor -eq 1 ]
then
cat <<END> file_palette_cpt.tmp
20	255	0	255	55	255	0	255
55	113	0	255	60	113	0	255
60	0	28	255	65	0	28	255
65	0	170	255	70	0	170	255
70	0	255	199	75	0	255	199
75	0	255	56	80	0	255	56
80	85	255	0	85	85	255	0
85	227	255	0	90	227	255	0
90	255	142	0	95	255	142	0
95	255	0	0	120	255	0	0
120	170	0	0	200	170	0	0
END
else
cat <<END> file_palette_cpt.tmp
50  	0       0       0       55	0       0       0
55 	28      28      28      60	28      28      28
60 	57      57      57      65	57      57      57
65 	85      85      85      70	85      85      85
70	113     113     113     75	113     113     113
75	142     142     142     80	142     142     142
80	170     170     170     85	170     170     170
85	200     200     200     90	200     200     200
90	227     227     227     95	227     227     227
95	255     255     255     100	255     255     255
END
fi

awk '{if($1!="#"){print $1,$2,$9 }}' $fileres > file_xy.tmp

xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
psbasemap -R -Y-6.5 -Jx -B$Bx_tics_4/$By_tics_4:" ":Wnes -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi
#grdsample litosfera_grd.tmp -Gfile_grd_sample.tmp -N100/100 -V -R
#grd2cpt $file_grd > file_palette_cpt.tmp
#grd2cpt $file_grd -C300/0 -L60/160 -S60/360/20 -V > file_palette_cpt.tmp
#grdview $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -Qi50 -V >> $FILEPS
grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
#pscoast -R -Jx -Di -A0/1 -W10 -K -O -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
grdcontour $file_grd -Bnsew -C10 -A20f6 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D13/2.6/5.5/.3 -B:"  ": -O -K -V >> $FILEPS
pstext -R -Jx -O -N -V <<END>> $FILEPS
$xtit $ytit  12 0 4 2 Surface heat flow (mW/m@+2@+)
END

rm -f file_*.tmp

fi


if [ $selec -eq 14 ]	###  elevation, sediment, crustal and lithospheric mantle thicknesses 
then
FILEPS=geometry.ps
rm -f $FILEPS
#echo " "
#echo 'Do you want to aplied a Gaussian filter for the data ?  1-yes' 
#read Sifilter
Sifilter=5
   if [ $Sifilter -eq 1 ]
   then
	echo 'Gaussian wide in km?' 
	read Gaussian
   fi
Tcolor=1	# Tcolor=1 -> Color,	Tcolor=2 -> Black and white

##----------  PRIMERA - elevacio ----------

awk '{if($1!="#"){print ($1,$2,$3) }}' $fileres > file_xy.tmp
text_e="elevation (km)"
if [ -f $file_tisc$ext_res ]							# elevation from tisc
then
   echo "  -------------------  Do you want to plot the elevation resulting from tisc? 1-Yes ------ "
   read eflex
if [ $eflex -eq 1 ]
then   
   awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_tisc$ext_res > file_topo.tmp
   sort -k 2,2n -k 1,1n -o file_xy.tmp file_topo.tmp
   text_e="elevation - tisc (km)"
fi
fi

echo " Use the measured topography from the grid" 
echo $file_topodata
echo " 1-Yes "
#read Var_topodata
Var_topodata=2

xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
cp file_grd.tmp filetopo_grd.tmp
if [ $Tcolor -eq 1 ]
then
   cp filePalette_cpt_color2.tmp file_palette_cpt.tmp
else
   cp filePalette_cpt_BW.tmp file_palette_cpt.tmp
fi 
#pstext titol.tmp -P -R -X1 -Y25 -Jx$scale_4Fig -N -K -V > $FILEPS
#pstext -P -R -X1 -Y25 -Jx$scale_4Fig -N -K -V <<END> $FILEPS
#$x2vel $y2vel 16 0 4 2  $TIME_My My
#END
psbasemap -Y21 -X2 -P -R -Jx$scale_4Fig -B$Bx_tics_4/$By_tics_4:" ":WNes -K -V > $FILEPS
#psbasemap -Y-4.5 -X1 -R -Jx$scale_4Fig -B$Bx_tics_4/$By_tics_4:" ":WNes -K -O -V >> $FILEPS
xtext=$(($x2vel+$x2vel/5))
ytext=$(($y2vel+$y2vel/10))
#ytext=$(($y2vel+6*$y1))
pstext -R -Jx -N -K -O -V <<END>> $FILEPS
$xtext $ytext 16 0 4 1  $TIME_My My
END
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi
#grd2cpt $file_grd -Crelief > file_palette_cpt.tmp
#grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
grdview $file_grd -Jx -R -Qs -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
#pscoast -R -Jx -Di -A0/1 -W10 -K -O -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W2/0/0/0t35_5_0_0:5 -V >> $FILEPS
psxy $fcorba2 -R -O -K -Jx -Sc0.2 -G255 -W1/0 -N -V >> $FILEPS

if [ $Var_topodata -eq 1 ]
then   
   grdcontour $file_topodata -Bnsew -W10/0 -C5 -Jx -R -O -K -V >> $FILEPS
   grdcontour $file_topodata -Bnsew -W3/0 -C0.5 -A1f9 -G4/8 -Jx -R -O -K -V >> $FILEPS
else
   grdcontour $file_grd -Bnsew -C0.5 -A1f6 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
fi
psscale -Cfile_palette_cpt.tmp -L -D15.8/3/5.6/.3 -B:" ": -O -K -V >> $FILEPS
pstext -R -Jx -O -K -N -V <<END>> $FILEPS
$xtit $ytit 11 0 4 2  $text_e
#$xtit $ytitsota 12 0 4 2 (km)
END

rm -f file_*.tmp

#echo "Drainage: Which time step? (que sigui el mateix que el fitxer de resultats)"  # drainage
#read timestep 
timestep=$ext_res
name=res_tisc
tmp=file_
col_river=0/0/255

#size of the lakes depending on domain area:
area=$(($x2vel*$y2vel))
#disch1=$(($area/90000))
disch1=200

if [ -f $name$timestep.xyw ] 
then

awk '{if ($3>100 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W1/$col_river -H2 -O -K >> $FILEPS 
awk '{if ($3>500 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W4/$col_river -H2 -O -K >> $FILEPS 
awk '{if ($3>3000 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W8/$col_river -H2 -O -K >> $FILEPS 
#Llacs
cat - <<END>  $tmp.lakes_cpt.tmp
	0	0	0	0	.49	0	0	0
	.49	0	40	255	1	0	40	255
END
awk -v sl=$sea_level '(substr($0,1,1)!="#"){if (substr($0,1,1)==">") disch=$11; else{if (disch>dl && ($3=="L" || $3=="E")) print $1,$2,1; else print $1,$2,0}}' dl=$disch1 $name$timestep.lak | \
	xyz2grd -G$tmp.lakes.grd.tmp -I$x1/$y1 -R -H2 -N0
grdclip $tmp.lakes.grd.tmp -G$tmp.lakes2.grd.tmp -Sb0.5/NaN
grdimage $tmp.lakes2.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -Q -O -K >> $FILEPS
grdcontour $tmp.lakes.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -W2/$col_river -O -K >> $FILEPS

#Llacs endorheics
cat - <<END>  $tmp.lakes_cpt.tmp
	0	0	0	0	.49	0	0	0
	.49	20	20	255	1	20	20	255
END
awk -v sl=$sea_level '(substr($0,1,1)!="#"){if (substr($0,1,1)==">") {disch=$11; if ($9!=0) disch=0} else{if (disch>dl && ($3=="L" || $3=="E")) print $1,$2,1; else print $1,$2,0}}' dl=$disch1 $name$timestep.lak | \
	xyz2grd -G$tmp.lakesendorh.grd.tmp -I$x1/$y1 -R -H2 -N0
grdclip $tmp.lakesendorh.grd.tmp -G$tmp.lakes2.grd.tmp -Sb0.5/NaN
grdimage $tmp.lakes2.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -Q -O -K >> $FILEPS
grdcontour $tmp.lakesendorh.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -W2/$col_river -O -K >> $FILEPS

fi

##  total sedimentat i erosionat
awk '{	if($5=="Time") time=$7; \
	if($1=="eros_nosd:" && time<=TIME_My) {totaleros+=$2; totalsed+=$5} }
	END {printf "%.3e  %.3e", totaleros/9.81, totalsed/9.81}' \
	TIME_My=$TIME_My $f_screen | read totaleros totalsed

##----------  SEGONA -  sediment thickness ----------
#awk '{if($1!="#"){print ($1,$2,$4) }}' $fileres > file_xy.tmp
#xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1

file_eros=res_erosion$timestep
min_sed=0.2	# Minimum sediment thickness
awk '{if($1!="#") {print $1/1e3,$2/1e3,-1*$3/1e3 } }' $file_eros > file_eros1.tmp	# km
sort -k 2,2n -k 1,1n -o file_eros.tmp file_eros1.tmp
wc file_eros.tmp | read row_eros a b c
awk '{if($1!="#") {printf " %11.3f %11.3f %10.4f\n ",$1,$2,$4 }}' $fileres > file_sediments2.tmp
awk '{if(NR<=row_eros) {print $0 }}' row_eros=$row_eros file_sediments2.tmp > file_sediments1.tmp
sort -k 2,2n -k 1,1n -o file_sediments.tmp file_sediments1.tmp
paste file_eros.tmp file_sediments.tmp > file_eros_sed.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_eros_sed.tmp 
awk '{if($6>min_sed) {print $1,$2,$6} else {print $1,$2,$3} }' \
	min_sed=$min_sed file_eros_sed.tmp > file_xy.tmp
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
#awk '{if($6>0) sedvol+=$6; else erosvol+=$3}END{printf "sedvol, erosvol = %.2e, %.2e  km3  ", sedvol*x1*y1, erosvol*x1*y1}' \
#	x1=$x1 y1=$y1 file_eros_sed.tmp
awk '{if($6>0) sedvol+=$6; else erosvol+=$3}END{printf "%.2e, %.2e", sedvol*x1*y1, -erosvol*x1*y1}' \
	x1=$x1 y1=$y1 file_eros_sed.tmp | read sedvol erosvol
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1

#grd2cpt $file_grd > filecpt_Sediment-Erosio.tmp
psbasemap -Y-6.5 -R -Jx -B$Bx_tics_4/$By_tics_4:" ":Wnes -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi
#grdsample litosfera_grd.tmp -Gfile_grd_sample.tmp -N100/100 -V -R
#grdimage $file_grd -Jx -R -O -K -Cfilecpt_Sediment-Erosio.tmp -V >> $FILEPS
grdview $file_grd -Jx -R -Qs -O -K -Cfilecpt_Sediment-Erosio.tmp -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
#grdcontour $file_grd -Bnsew -C2 -W1 -A2f6 -L0.2/7 -G4/8 -O -K -Jx -R -V >> $FILEPS
if [ $Var_topodata -eq 1 ]
then   
   grdcontour $file_topodata -Bnsew -W10/0 -C5 -Jx -R -O -K -V >> $FILEPS
fi
psscale -Cfilecpt_Sediment-Erosio.tmp -L -D15.8/3/5.6/.3 -B:"  ": -O -K -V >> $FILEPS
xtexteros=$(($x2vel+3*$x1))
pstext -R -Jx -O -K -N -V <<END>> $FILEPS
$xtit $ytit 11 0 4 2 erosion(-)/sediment(+) thickness (km)
#$xtit $ytitsota 12 0 4 2 thickness (km)
$xtexteros 500 11 0 4 1 totaleros= $totaleros kg,   erosvol=$erosvol km3
$xtexteros 100 11 0 4 1 totalsed= $totalsed kg,   sedvol=$sedvol km3



END

rm -f file_*.tmp 

#-----  TERCERA -  gruix cortical -----
text1="crustal"
text2="thickness (km)"
awk '{if($1!="#"){print $1,$2,$5} }' $fileres > file_xy.tmp

#text1="crustal + sediment"
#text2="thickness (km)"
#awk '{if($1!="#"){print ($1,$2,$5+$4)} }' $fileres > file_xy.tmp

#text1="Moho"
#text2="depth (km)"
#awk '{if($1!="#"){print ($1,$2,$5+$4-$3)} }' $fileres > file_xy.tmp


xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
if [ $Tcolor -eq 1 ]
then
cat <<END> file_palette_cpt.tmp
15	255	200	255	24	255	200	255
24	255	0	255	26	255	0	255
26	200	0	255	28	200	0	255
28	113	0	255	30	113	0	255
30	0	28	255	32	0	28	255
32	0	170	255	34	0	170	255
34	0	255	199	36	0	255	199
36	0	255	56	38	0	255	56
38	85	255	0	40	85	255	0
40	227	255	0	42	227	255	0
42	255	142	0	46	255	142	0
46	255	0	0	55	255	0	0
55	170	0	0	80	170	0	0
END
else
cat <<END> file_palette_cpt.tmp
5  	0       0       0       15	0       0       0
15 	28      28      28      24	28      28      28
24 	57      57      57      29	57      57      57
29 	85      85      85      33	85      85      85
33	113     113     113     37	113     113     113
37	142     142     142     41	142     142     142
41	170     170     170     45	170     170     170
45	200     200     200     50	200     200     200
50	227     227     227     55	227     227     227
55	255     255     255     100	255     255     255
END
fi
psbasemap -Y-6.5 -R -Jx -B$Bx_tics_4/$By_tics_4:" ":Wnes -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi
#grdsample litosfera_grd.tmp -Gfile_grd_sample.tmp -N100/100 -V -R
#grd2cpt $file_grd > file_palette_cpt.tmp
#grdview $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -Qi50 -V >> $FILEPS

cat <<END> file_palette_cpt.tmp
15 0 0 80 26 0 0 80
END
#makecpt -Cjet -T25/52/3 >> file_palette_cpt.tmp 
makecpt -Cjet -T26/48/2 >> file_palette_cpt.tmp 
cat <<END>> file_palette_cpt.tmp
48 120 0 0 70 120 0 0
END

#makecpt -Cjet -T28/58/4 > file_palette_cpt.tmp 

if [ $Mod_selec -eq 3 ]
then
cat <<END> file_palette_cpt.tmp
15 0 0 80 24 0 0 80
END
makecpt -Cjet -T24/60/3 >> file_palette_cpt.tmp 
cat <<END>> file_palette_cpt.tmp
60 120 0 0 80 120 0 0
END
fi

#grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
grdview $file_grd -Jx -R -Qs -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
grdcontour $file_grd -Bnsew -C5 -A10f6 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
if [ $Var_topodata -eq 1 ]
then   
   grdcontour $file_topodata -Bnsew -W10/0 -C5 -Jx -R -O -K -V >> $FILEPS
fi
psscale -Cfile_palette_cpt.tmp -L -D15.8/3/5.6/.3 -B:"  ": -O -K -V >> $FILEPS
pstext -R -Jx -O -K -N -V <<END>> $FILEPS
$xtit $ytit 12 0 4 2 $text1 $text2
#$xtit $ytitsota 12 0 4 2 $text2
END

rm -f file_*.tmp 

##----------  QUARTA  - gruix mantell litosferic ----------
text1="lithospheric mantle"
text2="thickness (km)"
awk '{if($1!="#"){print $1,$2,$6-$4-$5 }}' $fileres > file_xy.tmp 
makecpt -Cjet -T48/132/7 > file_palette_cpt.tmp 
cat <<END>> file_palette_cpt.tmp
132 120 0 0 150 120 0 0
END

#text1="lithospheric"	##----------  gruix litosfera ----------
#text2="thickness (km)"
#awk '{if($1!="#"){print $1,$2,$6 }}' $fileres > file_xy.tmp 
#cat <<END> file_palette_cpt.tmp				#### Tibetan Plateau
#50  90 0 0	70 90 0 0
#END
#makecpt -Cjet -I -T70/200/10 >> file_palette_cpt.tmp 
#cat <<END>> file_palette_cpt.tmp
#200  0 0 80	230 0 0 80
#END
#makecpt -Cjet -I -T70/190/20 > file_palette_cpt.tmp 


#text1="lithospheric"
#text2="depth (km)"
#awk '{if($1!="#"){print $1,$2,$6-$3 }}' $fileres > file_xy.tmp 


#if [ $Tcolor -eq 1 ]
#then
#cat <<END>file_palette_cpt.tmp
#10	255	200	255	50	255	200	255
#50	255	0	255	60	255	0	255
#60	113	0	255	70	113	0	255
#70	0	28	255	80	0	28	255
#80	0	170	255	90	0	170	255
#90	0	255	199	100	0	255	199
#100	0	255	56	110	0	255	56
#110	85	255	0	120	85	255	0
#120	227	255	0	130	227	255	0
#130	255	142	0	150	255	142	0
#150	255	0	0	200	255	0	0
#END
#else
#cat <<END>file_palette_cpt.tmp
#10  	0       0       0       20	0       0       0
#20 	28      28      28      30	28      28      28
#30 	57      57      57      40	57      57      57
#40 	85      85      85      50	85      85      85
#50	113     113     113     60	113     113     113
#60	142     142     142     70	142     142     142
#70	170     170     170     80	170     170     170
#80	200     200     200     90	200     200     200
#90	227     227     227     100	227     227     227
#100	255     255     255     150	255     255     255
#END
#fi

xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
psbasemap -R -Y-6.5 -Jx -B$Bx_tics_4/$By_tics_4:" ":WneS -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi
#grdsample litosfera_grd.tmp -Gfile_grd_sample.tmp -N100/100 -V -R
#grd2cpt $file_grd > file_palette_cpt.tmp
#grd2cpt $file_grd -C300/0 -L60/160 -S60/360/20 -V > file_palette_cpt.tmp
#grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
grdview $file_grd -Jx -R -Qs -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
if [ $Var_topodata -eq 1 ]
then   
   grdcontour $file_topodata -Bnsew -W10/0 -C5 -Jx -R -O -K -V >> $FILEPS
fi
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
grdcontour $file_grd -Bnsew -C15 -A30f6 -G10/8 -O -K -Jx -R -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D15.8/2.9/5.6/.3 -B:"  ": -O -K -V >> $FILEPS

if [ $Sifilter -eq 1 ]
then
pstext -R -Jx -O -K -N -V <<END>> $FILEPS
1700 0 10 0 4 0 Gaussian filter $Gaussian km
END
fi

pstext -R -Jx -O -K -N -V <<END>> $FILEPS
$xtit $ytit 12 0 4 2 $text1 $text2
#$xtit $ytitsota 12 0 4 2 $text2
END

pstext -R0/8/0/8 -Jx3 -O -N -V <<END>> $FILEPS
-0.4 9.1 11 0 0 0 $DIR_main
END

grdsample file_grd.tmp -Gfile_2grd.tmp -I44.108/33.358 -R -V
grd2xyz file_2grd.tmp > file_L.tmp
sort -k 2,2n -k 1,1n -o fileL_ord.tmp file_L.tmp
rm -f file_*.tmp

fi

if [ $selec -eq 15 ]		###  Vorticity
then
FILEPS=Vorticity.ps
file_rot=$f_rotation$ext_res
rm -f $FILEPS
Regio_graf=$regio

#text_scale="clockwise(-)        Vorticity (exp(-16) rad/s)        counterclockwise (+)"	## rad/s-1
text_scale=" counterclockwise        Vorticity        clockwise"	## rad/s-1
awk '{if($1!="#"){print $1,$2,-$7*1e16 }}' $fepunt12 > file_zxy.tmp   #### exp(-16) rad/s-1

#time_int=1
#text_scale="clockwise(-)        Vorticity (degree/$time_int Myear)        counterclockwise (+)"	## degree/My
#awk '{if($1!="#"){print $1,$2,time_int*($7*31536000*1e6*180)/3.1416 }}' time_int=$time_int $fepunt12 > file_zxy.tmp			## degree/My

xyz2grd file_zxy.tmp -R$regio -Gfile_grd.tmp -I$x1/$y1
cat <<END> file_palette_cpt.tmp
-20	0	0	80	-10	0	0	80
-10	0	0	150	-3	0	0	150
-3	57	57	220	-2	57	57	220
-2	113	113	255	-1	113	113	255
-1	170	170	255	-0.5	170	170	255
-0.5	227	227	255	0	227	227	255
0	255	227	227	0.5	255	227	227
0.5	255	170	170	1	255	170	170
1	255	113	113	2	255	113	113
2	220	57	57	3	220	57	57
3	150	0	0	10	150	0	0
10	80	0	0	20	80	0	0
END

#grd2cpt file_grd.tmp > file_palette_cpt.tmp
#makecpt -Chaxby -T1.29/3.99/0.18 > file_palette_cpt.tmp
#make_polar_cpt.job <<END   ## Crea la paleta de color
#file_zxy.tmp
#END
#makecpt -Chaxby -T-100/40/10 > file_palette_cpt.tmp
#cat <<END> file_palette_cpt.tmp
#-10 210 210 210 0 210 210 210
#END
#makecpt -Chaxby -I -T0/60/5 >> file_palette_cpt.tmp
#cat <<END>> file_palette_cpt.tmp
#60 0 0 100 90 0 0 100
#END

pstext titol.tmp -R$regio_vel -Y14.5 -X3 -JX10/5.5 -N -K -V > $FILEPS
grdimage file_grd.tmp -Y-10.5 -Jx$scale_1Fig -R$Regio_graf -Cfile_palette_cpt.tmp -O -K -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D10/-1.5/16/.3h -B:"  $text_scale  ": -O -K -V >> $FILEPS
psbasemap -R -Jx -B$Bx_tics:" $DIR_main ":N -V -K -O >> $FILEPS

awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xy_cont.tmp	# elevation
contour_text=" contours elevation (km), circles (degrees)"
xyz2grd file_xy_cont.tmp -R$regio_vel -Gfile_crust_grd.tmp -I$x1/$y1
grdcontour file_crust_grd.tmp -C0.5 -A1 -G4/8 -W1/0 -O -K -Jx -B$Bx_tics:" $contour_text ":/$By_tics:"y (km)":enWS -R$Regio_graf -V >> $FILEPS
 
psxy $fcorba1 -R -O -K -M -L -Jx -W10/0/0/0t35_35_0_0:5 -N -V >> $FILEPS
#psxy $fcorba2 -R -O -K -Jx -Sc0.2 -G255 -W1/0 -N -V >> $FILEPS

###   surface velocity:
#awk '{if($3!=0 || $4!=0){print $1,$2,(180/3.1416)*atan2($4,$3),sqrt($3*$3+$4*$4)} \
#	else {print $1,$2,0,0}}' $fvelocity > file_az_vel.tmp
#awk '{if($4>=vmin){print ($1,$2,$3,$4/facvel)} }' \
#	facvel=$facvel vmin=$vmin file_az_vel.tmp > file_vel1.tmp
#awk ' BEGIN {linia=1}
#       {if(NR==linia) {print $1,$2,$3,$4; linia=linia+160} }' file_vel1.tmp > file_vel.tmp
#cat <<END>>file_vel.tmp
#10 -220 0 2
#END
#tic_facvel=$(($facvel*2)) 
#psxy file_vel.tmp -R -K -O -N -Jx -Sv.08/0.38/0.15n0.5 -G0 -W1/255 -V >> $FILEPS
#pstext -R -Jx -O -N -V <<END>> $FILEPS
#10 -200 11 0 0 0 $tic_facvel mm/year
#END
###


###  Finite rotations
#awk '{if($1!="#" && $3<-2) {print $1,$2,$3 } }' $file_rot > file_zxy.tmp
awk '{if($1!="#") {print $1,$2,$3 } }' $file_rot > file_zxy.tmp
awk '{printf " %10.2f %10.2f %2i %2i %2i %2i %4i \n ", $1,$2,10,0,1,6,-$3 }' file_zxy.tmp > file_Timetext.tmp
cat <<END> file_palette_cpt.tmp
-150	0	0	200	-60	0	0	200
-60	60	60	220	-30	60	60	220
-30	120	120	255	-15	120	120	255
-15	180	180	255	-5	180	180	255
-5	240	240	255	0	240	240	255
0	255	227	227	5	255	227	227
5	255	170	170	10	255	170	170
10	255	113	113	20	255	113	113
20	220	57	57	60	220	57	57
60	200	0	0	150	200	0	0
END
psxy file_zxy.tmp -R -Bnsew -Jx -Sc0.6 -G255 -W5/0 -O -K -V >> $FILEPS
#ppsxy file_zxy.tmp -R -Bnsew -Jx -Sc0.6 -Cfile_palette_cpt.tmp -W5/255 -N -O -K -V >> $FILEPS
#psscale -Cfile_palette_cpt.tmp -L -D7/-2/13/.3h -B:" $text_scale ": -O -K -V >> $FILEPS
pstext file_Timetext.tmp -R -Jx -O -V >> $FILEPS

fi


if [ $selec -eq 16 ]
then
###########################################################################
################ Moho Temperature +  Lithospheric thickness  #######################
echo Moho Temperature
FILEPS=Tm_L.ps
rm -f $FILEPS
						#############################################  PANNEL 1
psbasemap -Y16 -X4 -P -R -Jx$scale_1Fig -B$Bx_tics_4:" $DIR_main ":/$By_tics_4:" ":wNes -K -V > $FILEPS		#### Moho Temperature
awk '{if($1!="#"){print $1,$2,$7-273 } }' $fileres > file_Tmoho.tmp
xyz2grd file_Tmoho.tmp -R -Gfile_Tmoho_grd.tmp -I$x1/$y1
makecpt -Cjet -T360/580/20 > file_palette_cpt.tmp 
#grd2cpt file_Tmoho_grd.tmp > file_palette_cpt.tmp
grdimage file_Tmoho_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
#grdview file_Tmoho_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -Qi50 -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D6.3/-0.3/13/.3h -B:" T@-moho@-(C) ": -O -K -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
awk '{if($1!="#"){print $1,$2,$5 } }' $fileres > file_xycrust.tmp		## crust
xyz2grd file_xycrust.tmp -R$regio_vel -Gfile_crust_grd.tmp -I$x1/$y1
contour_text="contours crustal thickness (km)"
grdcontour file_crust_grd.tmp -Jx -R -B$Bx_tics:"  ":/$By_tics:" $contour_text ":nEsw -C5 -A10f8 -G8/8 -W1/0 -O -K -V >> $FILEPS
rm -f file_*.tmp
						#############################################  PANNEL 2
#var_text="lithospheric mantle thickness"    			#####  lithospheric mantle thickness
#awk '{if($1!="#"){print $1,$2,$6-$4-$5 }}' $fileres > file_xy.tmp
#cat <<END> file_palette_cpt.tmp
#0 120 0 0 20 120 0 0
#END
#makecpt -Cjet -I -T20/110/10 >> file_palette_cpt.tmp 
#cat <<END>> file_palette_cpt.tmp
#110 0 0 80 150 0 0 80
#END

var_text="lithospheric thickness"  				#####   lithospheric thickness
awk '{if($1!="#"){print $1,$2,$6 }}' $fileres > file_xy.tmp 
makecpt -Cjet -I -T60/250/20 > file_palette_cpt.tmp 
cat <<END>> file_palette_cpt.tmp
F   0  0  100
B   100  0  0
END

#var_text="lithospheric depth"					#####   lithospheric depth
#awk '{if($1!="#"){print $1,$2,$6-$3 }}' $fileres > file_xy.tmp

psbasemap -R -Y-14 -Jx -B$Bx_tics_4/$By_tics_4:" ":wnes -K -O -V >> $FILEPS
xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
file_grd=file_grd.tmp
grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
#grdcontour $file_grd -Bnsew -C2 -A4f6 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D6.3/-0.3/13/.3h -B:" $var_text (km) ": -O -K -V >> $FILEPS
#pstext -R -Jx -Bnsew -O -N -V <<END>> $FILEPS
#$xtit $ytit  12 0 4 2 $var_text (km)
#END

awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_tisc$ext_res > file_topo.tmp		####  elevation from tisc
sort -k 2,2n -k 1,1n -o file_xy.tmp file_topo.tmp
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
grdcontour file_grd.tmp -Jx -R -B$Bx_tics:"  ":/$By_tics:" contours elevation ":nEsw -C1 -A2f8 -G8/8 -W1/0 -O -V >> $FILEPS

rm -f file_*.tmp
fi



if [ $selec -eq 17 ]	###############  ELEVATION + velocities   ##################
then
FILEPS=elevation.ps
rm -f $FILEPS
Sifilter=5
   if [ $Sifilter -eq 1 ]
   then
	echo 'Gaussian wide in km?' 
	read Gaussian
   fi
Tcolor=1	# Tcolor=1 -> Color,	Tcolor=2 -> Black and white


awk '{if($1!="#"){print ($1,$2,$3) }}' $fileres > file_xy.tmp
awk '{if(NR==1) {printf("%.1f", $4) } }' $fileres | read TIME_My
text_e="elevation (km)"
if [ -f $file_tisc$ext_res ]							# elevation from tisc
then
   echo "  -------------------  Do you want to plot the elevation resulting from tisc? 1-Yes ------ "
   read eflex
if [ $eflex -eq 1 ]
then   
   awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_tisc$ext_res > file_topo.tmp
   sort -k 2,2n -k 1,1n -o file_xy.tmp file_topo.tmp
   text_e="elevation - tisc (km)"
fi
fi

xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
cp file_grd.tmp filetopo_grd.tmp
line_jump=$((0.7*$x2vel/$x1))
yTime=1500
cp filePalette_cpt_color2-topo.tmp file_palette_cpt.tmp
#pstext titol.tmp -P -R -X1 -Y25 -Jx$scale_4Fig -N -K -V > $FILEPS
psbasemap -Y4 -X3 -R -Jx$scale_2Fig -B$Bx_tics:" ":/$By_tics:" km ":WNes -K -V > $FILEPS
pstext -R -Jx -N -K -O -V <<END>> $FILEPS
800 $yTime 16 0 4 2  $TIME_My My
END
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi
#grd2cpt $file_grd -Crelief > file_palette_cpt.tmp
grdgradient $file_grd -Gfile_grd.intensity.tmp -A180 -V 
area=$(($x2vel*$y2vel))
grdmath file_grd.intensity.tmp $area DIV 1000000000 MUL = file_grd.intensity.tmp
grdimage $file_grd -Jx -R -Cfile_palette_cpt.tmp -Ifile_grd.intensity.tmp -O -K -V >> $FILEPS
#grdimage $file_grd -Jx -R -Cfile_palette_cpt.tmp -O -K -V >> $FILEPS
#pscoast -R -Jx -Di -A0/1 -W10 -K -O -V >> $FILEPS
#psxy $fcorba1 -R -M -L -Jx -W10/80/80/80 -O -K -V >> $FILEPS		### Blocs
##psxy $fcorba2 -R -Bg500 -O -K -Jx -Sc0.2 -G255/200/200 -W1/0 -N -V >> $FILEPS
#psscale -Cfile_palette_cpt.tmp -L -D15.8/2.7/5/.3 -B:" ": -O -K -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D5.4/-0.3/10/.25h -B:" Elevation (km) ": -O -K -V >> $FILEPS

cp $file_grd filetopo.tmp
#echo "Drainage: Which time step? (que sigui el mateix que el fitxer de resultats)"  # drainage
#read timestep 
timestep=$ext_res
name=res_tisc
tmp=file_
col_river=0/0/255

#size of the lakes depending on domain area:
area=$(($x2vel*$y2vel))
#disch1=$(($area/90000))
disch1=200

if [ -f $name$timestep.xyw ] 
then
awk '{if ($3>100 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W1/$col_river -H2 -O -K >> $FILEPS 
awk '{if ($3>500 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W4/$col_river -H2 -O -K >> $FILEPS 
awk '{if ($3>3000 && NR>2 && $5!="S" && $5!="L") {print $1, $2; print $7, $8; print ">"}}' $name$timestep.xyw | \
	psxy -Jx -R -M -W8/$col_river -H2 -O -K >> $FILEPS 
#Llacs
cat - <<END>  $tmp.lakes_cpt.tmp
	0	0	0	0	.49	0	0	0
	.49	0	140	255	1	0	140	255
END
awk -v sl=$sea_level '(substr($0,1,1)!="#"){if (substr($0,1,1)==">") disch=$11; else{if (disch>dl && ($3=="L" || $3=="E")) print $1,$2,1; else print $1,$2,0}}' dl=$disch1 $name$timestep.lak | \
	xyz2grd -G$tmp.lakes.grd.tmp -I$x1/$y1 -R -H2 -N0
grdclip $tmp.lakes.grd.tmp -G$tmp.lakes2.grd.tmp -Sb0.5/NaN
grdimage $tmp.lakes2.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -Q -O -K >> $FILEPS
grdcontour $tmp.lakes.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -W2/$col_river -O -K >> $FILEPS

#Llacs endorheics
cat - <<END>  $tmp.lakes_cpt.tmp
	0	0	0	0	.49	0	0	0
	.49	80	120	255	1	80	120	255
END
awk -v sl=$sea_level '(substr($0,1,1)!="#"){if (substr($0,1,1)==">") {disch=$11; if ($9!=0) disch=0} else{if (disch>dl && ($3=="L" || $3=="E")) print $1,$2,1; else print $1,$2,0}}' dl=$disch1 $name$timestep.lak | \
	xyz2grd -G$tmp.lakesendorh.grd.tmp -I$x1/$y1 -R -H2 -N0
grdclip $tmp.lakesendorh.grd.tmp -G$tmp.lakes2.grd.tmp -Sb0.5/NaN
grdimage $tmp.lakes2.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -Q -O -K >> $FILEPS
grdcontour $tmp.lakesendorh.grd.tmp -Jx -R -C$tmp.lakes_cpt.tmp -W2/$col_river -O -K >> $FILEPS

dist_rose=112	#160
y_rose1=$((350-$dist_rose))
y_rose2=$((350+$dist_rose))
#psxy -R -M -Jx -W8/120/120/120ta -O -K -V <<END>> $FILEPS
#0	$y_rose1
#700	$y_rose1
#>
#700	$y_rose2
#0	$y_rose2
#END
fi

echo " Use the measured topography from the grid or the velocity field" 
echo $file_topodata
echo " 1- Data topography	2- Velocity field 	3- Non"
#read Var_topodata
Var_topodata=3
if [ $Var_topodata -eq 1 ]
then
   grdcontour $file_topodata -W13/0 -C5 -Jx -R -O -K -V >> $FILEPS
   grdcontour $file_topodata -W4/0 -C0.5 -A1f10 -G6/8 -Jx -R -O -K -V >> $FILEPS
else
   #grdcontour $file_grd -Bnsew -C0.5 -A1f6 -G8/8 -O -K -Jx -R -V >> $FILEPS
   echo " OFF plotting elevation contours"
fi

if [ $Var_topodata -eq 2 ]
then
vmin=0   	#colapse orogenic
x_symbol=$((-3*$x1)) 
awk '{if($3!=0 || $4!=0){print $1,$2,(180/3.1416)*atan2($4,$3),sqrt($3*$3+$4*$4)} \
	else {print $1,$2,0,0}}' $fvelocity > file_az_vel.tmp

awk '{if($4>=vmin){print ($1,$2,$3,$4/facvel)} }' \
	facvel=$facvel vmin=$vmin file_az_vel.tmp > file_vel1.tmp
awk ' BEGIN {linia=1}
       {if(NR==linia) {print $1,$2,$3,$4; linia=linia+line_jump} }' line_jump=$line_jump file_vel1.tmp > file_vel.tmp
cat <<END>> file_vel.tmp
$x_symbol -$y1 180 2
END
tic_facvel=$(($facvel*2)) 
psxy file_vel.tmp -R -N -Jx -Sv.08/0.38/0.15n0.5 -G0 -W2/255 -K -O -V >> $FILEPS
#psxy $fcorba2 -R -O -K -Jx -Sc0.2 -G255 -W1/0 -N -V >> $FILEPS

pstext -R -Jx -O -K -N -V <<END>> $FILEPS
$x_symbol 0 12 0 0 3 $tic_facvel mm/year
#400 -200 11 0 0 0 $contour_text
END

#psbasemap -X-10 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:" Rain ":/$By_tics:" ":WNES -K -O -V > $FILEPS

fi

####  Plotting rain - Precipitation
rain_min=0.4
echo rain_min $rain_min
awk '{print ($1,$2,$10/1e3)}' $name$timestep.xyw | xyz2grd -Gfile_grd.tmp -I$x1/$y1 -R -H2
##makecpt -Ccool -T$rain_min/9/1 > file_cpt.tmp
cat <<END> file_cpt.tmp
$rain_min  255  70  70		2  255  0  0
2  255  0 0			5  180  0  0
5  180  0  0			8  70  0  0
B   255  150  150
F   70  0  0
END

grdclip file_grd.tmp -Gfile_grd.tmp -Sb$rain_min/NaN
grdimage file_grd.tmp -Cfile_cpt.tmp -Bnsew -J -R -Q -K -O >> $FILEPS
##psscale -Cfile_cpt.tmp -L -D5.5/-1.4/10/.2h -B:" rain (m/yr) ": -O -K -V >> $FILEPS



if [ -f $name$timestep.xyw ]	### Rose river orientation
then
pstext -R0/8/0/8 -Jx2 -O -K -N -V <<END>> $FILEPS
0 7.9 12 0 0 0 $DIR_main
4.9 5.9 12 0 0 0 time = $TIME_My My
END
	### Rose river orientation
	## cat $prj.xyw | outin $prj.INT_ROSE | awk '($1==1){print $4, atan2($9-$3,$8-$2)*180/3.1415927}' | \
   #awk '($2>=y_rose1 && $2<=y_rose2 && $3>4){print $3, 90-atan2($8-$2,$7-$1)*180/3.1415927}' y_rose1=$y_rose1 y_rose2=$y_rose2 $name$timestep.xyw | 
   #        psrose -S3.5n -R0/1/0/360 -A45 -X16 -Y4 -Ba.25g.25/a45g45:."river orientation": -G220 -O >> $FILEPS

else
pstext -R0/8/0/8 -Jx2 -O -N -V <<END>> $FILEPS
0 7.9 12 0 0 0 $DIR_main
END

fi



######  Erosion Rate
timestep=$ext_res
name=res_tisc
file_drai=$name$timestep.st

awk '{if(NR>2 && $1!="#") {print $1,$2,$4,$5 } }' $file_drai > file_eros_erosrate.tmp
#v_text="total erosion [km] results from tao (ojo, no inclou el terme advectiu!!)"
#awk '{print $1,$2,$3/1e3 }' file_eros_erosrate.tmp > file_eros.tmp	# Total erosion Km

v_text="Erosion Rate [mm/yr]"						# eros rate km/My
awk '{print $1,$2,$4/1e3 }' file_eros_erosrate.tmp > file_eros.tmp	# eros rate km/My
cat  <<END> file_palette_cpt.tmp
	-1.5       200   0     0     		-.5       255   0    0
	-.5       255   0     0     		0       255   255    255
	0   255    255     255     		.2 	30   180     255
	.2   30    180     255     		.5 	10   50     255
	.5   10   50     255     		1.3 	10   50     255
F	10	50	255
B	150	0	0
END
cat - <<END>  file_palette_cpt.tmp
	-10	255	0	0	-1	150	190	0
	-1	150	190	0	-0.1	255	230	60
	-0.1	255	230	60	0	255	255	255
	0	255	255	255	.01	21	234	255
END
makecpt -Ccool -T.01/10/1 -Qo -D >> file_palette_cpt.tmp

#makecpt -Chot -T.001/1/2 -Qo > file_palette_cpt.tmp
#makecpt -Cjet -T-.2/.6/0.05 > file_palette_cpt.tmp

psbasemap -X12 -R$regio_vel -Jx$scale_2Fig -B$Bx_tics:" ":/$By_tics:" km ":wNes -K -O -V >> $FILEPS
xyz2grd file_eros.tmp -R -Gfile_grd.tmp -I$x1/$y1
grdimage file_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
grdinfo file_grd.tmp
psscale -Cfile_palette_cpt.tmp -L -D5.4/-0.3/10/.25h -B:" $v_text ": -O -K -V >> $FILEPS
#psscale -Cfile_palette_cpt.tmp -L -D6/-2/12/.3h -B:" $v_text ": -O -K -V >> $FILEPS
#pstext -R -Jx -O -K -N -V <<END>> $FILEPS
#1500 $y2vel 11 0 0 0 $v_text
#END

#grdcontour filetopo.tmp -C0.5 -A1f6 -G8/8 -W2/100 -O -K -Jx -R -V >> $FILEPS

text_scale="vertical strain rate (10@+-16@+ s@+-1@+)"				##### Vertical Strain rate
awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres > file_zxy_1.tmp		
awk '{if($3>1.e-4 || $3<-1.e-4){print $1,$2,$3} else {print $1,$2,0}}' file_zxy_1.tmp > file_zxy.tmp		
xyz2grd file_zxy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
grdcontour file_grd.tmp -L2/1000 -W3/0 -C3 -Jx -R -Bnsew -O -V >> $FILEPS


rm -f file_*.tmp
fi

if [ $selec -eq 18 ]
then
############### Total eroded and erosion rate ##############################

#echo "Drainage: Which time step?"  # res_tisc.st
#read timestep 
timestep=$ext_res
name=res_tisc
file_drai=$name$timestep.st
file_eros=res_erosion$timestep

echo " Choose	1- Total erosion (km)"
echo " 		2- Erosion rate (km/My)"
read optero

if [ $optero -eq 1 ]
then
   FILEPS=Total_erosion.ps
   v_text="total erosion [km] results from uhuru "
   awk '{if($1!="#"){print $1/1e3,$2/1e3,-1*$3/1e3 } }' $file_eros > file_eros.tmp
cat <<END> file_palette_cpt.tmp
-8	80	0	0	-4	80	0	0
-4	150	0	0	-3	150	0	0
-3	220	57	57	-2	220	57	57
-2	255	113	113	-1	255	113	113
-1	255	170	170	-0.05	255	170	170
-0.05	255	227	227	0	255	227	227
0	227	227	255	0.5	227	227	255
0.5	170	170	255	1	170	170	255
1	113	113	255	2	113	113	255
2	57	57	220	3	57	57	220
3	0	0	150	4	0	0	150
4	0	0	80	10	0	0	80
END
#grd2cpt -Cpolar file_grd.tmp > file_palette_cpt.tmp
else
   FILEPS=erosion_rate.ps
   awk '{if(NR>2 && $1!="#") {print $1,$2,$4,$5 } }' $file_drai > file_eros_erosrate.tmp
   #v_text="total erosion [km] results from tao (ojo, no inclou el terme advectiu!!)"
   #awk '{print $1,$2,$3/1e3 }' file_eros_erosrate.tmp > file_eros.tmp	# Total erosion Km

   v_text="erosion rate [km/My]"						# eros rate km/Ma
   awk '{print $1,$2,$4/1e3 }' file_eros_erosrate.tmp > file_eros.tmp	# eros rate km/Ma
cat  <<END> file_palette_cpt.tmp
	-1.5       200   0     0     		-.1       255   0    0
	-.1       255   0     0     		0       120   120    120
	0   120    120     120     		.05 	30   180     255
	.05   30    180     255     		1.3 	10   50     255
F	10	50	255
B	150	0	0
END
   #makecpt -Chot -T.001/1/2 -Qo > file_palette_cpt.tmp
   #makecpt -Cjet -T-.2/.6/0.05 > file_palette_cpt.tmp
fi

rm -f $FILEPS
pstext titol.tmp -R$regio_vel -Y13.5 -JX10/5 -N -K -V > $FILEPS
psbasemap -Y-9.5 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:" x (km) ":/$By_tics:"y (km)":WEsN -V -K -O >> $FILEPS
xyz2grd file_eros.tmp -R -Gfile_grd.tmp -I$x1/$y1
grdimage file_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
grdinfo file_grd.tmp
psscale -Cfile_palette_cpt.tmp -L -D6.3/-0.3/13/.3h -B:" $v_text ": -O -K -V >> $FILEPS
#psscale -Cfile_palette_cpt.tmp -L -D6/-2/12/.3h -B:" $v_text ": -O -K -V >> $FILEPS
#pstext -R -Jx -O -K -N -V <<END>> $FILEPS
#1500 $y2vel 11 0 0 0 $v_text
#END

echo ' '
echo ' afegeix el gruix de sediments? 1-No=results from tao, ojo amb el terme advectiu'
read sedthck
if [ $sedthck -eq 1 ]
then
   psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
   #grdcontour file_grd.tmp -C1 -A2 -G2/8 -O -K -Bnsew -Jx -R -V >> $FILEPS
else
   awk '{if($1!="#") {print $1,$2,$4 }}' $fileres > file_xyz1.tmp
   awk '{if($3!=0) {print $1,$2,$3} else {print $1,$2,"NaN"} }' file_xyz1.tmp > file_xyz.tmp
   xyz2grd file_xyz.tmp -R -Gfile_grd.tmp -I$x1/$y1
   makecpt -Chot -T0/6/0.5 -I > file_palette_cpt.tmp
   grdview file_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -Qi50 -V >> $FILEPS
   psscale -Cfile_palette_cpt.tmp -L -D6/-1/12/.3h -B:" sediment thickness (km)": -O -K -V >> $FILEPS
   psxy $fcorba1 -R -O -K -M -L -Jx -W15/100/255/100t35_35_0_0:5 -V >> $FILEPS
   grdcontour file_grd.tmp -C0.5 -A1 -G2.5/8 -O -Jx -R -V >> $FILEPS
fi


pstext -R0/8/0/8 -Jx2 -O -N -V <<END>> $FILEPS
0 7.9 12 0 0 0 $DIR_main
END

fi


############### Total erosion / sediment thickness AND  Total Strain accumulated ##############################
if [ $selec -eq 19 ]
then
min_sed=0	# Minimum sediment thickness
xscale=$scale_1Fig
echo " Total strain accumulation "
FILEPS=StrainAccumulat.ps
timestep=$ext_res

pstext titol.tmp -N -R$regio_vel -Y13 -X4 -JX10/7 -N -K -V > $FILEPS
psbasemap -Y-9 -R$regio_vel -Jx$xscale -B$Bx_tics:" $DIR_main ":/$Bx_tics:"y (km)":WesN -V -K -O >> $FILEPS

##  Total Strain accumulation
file_Strain=$file_TotalStrain$timestep

#awk '{if(NR==1){print $14 } }' $file_Strain | read AverageStrain	### Calculate in UHURU.f

npoint=0
AverageStrain=0	
Yaverage_min=0e3	## meters						### If you want to considere just part of the model domain
echo "  "
echo "The average and the deviation calculation is done for y > " $Yaverage_min meters
echo " ok ?"
read ok
awk '{	if($1!="#" && $2>Yaverage_min) {AverageStrain+=$3; npoint=npoint+1 } } 
	END {printf "%6i   %.6f   %.6f", npoint, AverageStrain/npoint, Yaverage_min/1e3 }' \
	Yaverage_min=$Yaverage_min $file_Strain | read npoint AverageStrain Ykm_av

echo $npoint $AverageStrain $Ykm_av
npoint=0
StrainDev=0
awk '{	if($1!="#" && $2>Yaverage_min) {StrainDev+=($3-AverageStrain)*($3-AverageStrain); npoint=npoint+1 } }
	END {printf "%6i  %.6e", npoint, sqrt(StrainDev/npoint) } ' \
	Yaverage_min=$Yaverage_min AverageStrain=$AverageStrain $file_Strain | read npoint StrainDeviation
echo $npoint $StrainDeviation
awk '{if($1!="#"){print $1/1e3,$2/1e3,$3 } }' $file_Strain > file_xyz.tmp	# km
xyz2grd file_xyz.tmp -R -Gfile_grd.tmp -I$x1/$y1
cat <<END> file_palette_cpt.tmp
0	255	0	255	0.01	255	0	255
0.01	180	0	255	0.1	180	0	255
0.1	57	57	220	0.3	57	57	220
0.3	113	113	255	0.5	113	113	255
0.5	170	170	255	0.7	170	170	255
0.7	227	227	255	0.95	227	227	255
0.95	255	255	255	1.05	255	255	255
1.05	255	255	200	1.3	255	255	200
1.3	255	255	80	1.6	255	255	80
1.6	255	200	50	1.9	255	200	50
1.9	255	80	0	2.3	255	80	0
2.3	200	0	0	2.6	200	0	0
2.6	100	0	0	3	100	0	0
B       0       0       0
F       255     0       255
N       128     128     128
END
cat <<END> file_palette_cpt.tmp
0	50	0	100	0.6	50	0	100
END
makecpt -Chaxby -T0.6/2.8/.2 >> file_palette_cpt.tmp
#make_polar_cpt.job <<END   ## Crea la paleta de color
#file_xyz.tmp
#END
#grd2cpt file_grd.tmp > file_palette_cpt.tmp
grdview file_grd.tmp -Jx -R -B$Bx_tics:" Number of Points = $npoint ,  Average Accumulated Strain = $AverageStrain ,  Deviation = $StrainDeviation ":/$Bx_tics:" ":weSn  -Cfile_palette_cpt.tmp -Qi50 -V -O -K >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D6.5/-1.5/14/.3h -B:" Strain Accumulation ": -V -O -K >> $FILEPS
echo $Ykm_av
psxy -R -Jx -W10/0/0/0ta -N -O -K -V <<END>> $FILEPS
0	$Ykm_av
$x2vel	$Ykm_av
END
psxy $fcorba1 -R -O -K -M -L -Jx -Bnsew -W6/50/50/50 -V >> $FILEPS
#grdcontour file_grd.tmp -Jx -R -B$Bx_tics\g500:"  ":Sewn -C.05 -A.1 -W1/0 -O -V >> $FILEPS
awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xy_cont.tmp	# elevation
xyz2grd file_xy_cont.tmp -R -Gfile_grd.tmp -I$x1/$y1
contour_text=" contours elevation (km)"
grdcontour file_grd.tmp -Jx -R -B$Bx_tics\g500:" $contour_text ":sEwn -C1 -A2 -G2.5/8 -W1/0 -O -V >> $FILEPS


fi

if [ $selec -eq 20 ]  	############ initial lithospheric strength ##################
then
FILEPS=Strength_Lith.ps
rm -f $FILEPS

pstext titol.tmp -R$regio_vel -X3 -Y15 -JX10/5 -N -K -V > $FILEPS
tst=0
if [ $ext_res -eq 0 ]  	### Initial Bodies geometry
then
   echo 'Do you want just to plot the initial bodies geometry? 1- Yes '
   read tst
fi

if [ $tst -eq 1 ]  	### Initial Bodies geometry
then
   text_cpt=" Initial Bodies with different Strength"
   awk '{print $1/1e3,$2/1e3,$4 }' $f_Flit > file_Flit.tmp
   xyz2grd file_Flit.tmp -R -Gfile_grd.tmp -I$x1/$y1
cat <<END> file_palette_cpt.tmp
0.5	130 255 130	1.5	130 255 130
1.5	220 220 100	2.5	220 220 100
2.5	255 130 130	3.5	255 130 130
3.5	250 130 255	4.5	250 130 255
4.5	130 130 255	100	130 130 255
END
else
   text_cpt=" Lithospheric Strength (10@+12@+ N/m)"
   awk '{if($1!="#"){print $1,$2,$4/1e12 } }'  $file_Pav$ext_res > file_Flit.tmp
   xyz2grd file_Flit.tmp -R -Gfile_grd.tmp -I$x1/$y1
   #makecpt -Chaxby -T9/35/1.5 > file_palette_cpt.tmp
#cat <<END> file_palette_cpt.tmp
#0 0 0 80 5 0 0 80
#END
#makecpt -Cjet -T5/20/1 >> file_palette_cpt.tmp 
#cat <<END>> file_palette_cpt.tmp
#20 120 0 0 1e5 120 0 0
#END
grd2cpt -S0/51/4 file_grd.tmp > file_palette_cpt.tmp
#cat <<END>> file_palette_cpt.tmp
#50 120 0 0 1e10 120 0 0
#END
fi

psbasemap -Y-11 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:" ":/$By_tics:" y (km)":Wesn -V -K -O >> $FILEPS

grdimage file_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D11/-1.7/26/.3h -B:" $text_cpt ": -O -K -V >> $FILEPS
psxy $fcorba1 -R -O -K -N -M -L -Jx -W10/100/0/0t35_35_0_0:5 -V >> $FILEPS
#grdcontour file_grd.tmp -C10 -A20 -G2/8 -O -Bnsew -Jx -R -V >> $FILEPS
#cp file_grd.tmp Flit_tmp.grd

awk '{if($1!="#"){print ($1,$2,$5)} }' $fileres > file_xycrust.tmp			# crust
xyz2grd file_xycrust.tmp -R$regio_vel -Gfile_crust_grd.tmp -I$x1/$y1
contour_text=" contours crustal thickness (km)"
grdcontour file_crust_grd.tmp -Jx -R -B$By_tics:" $contour_text ":S -C5 -A10 -G8/8 -W1/0 -O -V >> $FILEPS
#pstext -R -Jx -O -N -V <<END>> $FILEPS
#400 -200 11 0 0 0 $contour_text
#END

fi

if [ $selec -eq 21 ]		### Finite Rotations 
then
text_scale="    clockwise(-)        Rotation [degree]        counterclockwise (+)"	## degree
file_rot=$f_rotation$ext_res
FILEPS=Finite_Rotations.ps
pstext titol.tmp -R$regio_vel -Y14.7 -X2 -JX10/5.5 -N -K -V > $FILEPS

awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xyz.tmp		## elevation
contour_text=" contours elevation [km]"

#psbasemap -Y-9 -G255 -R$Regio_graf -Jx.01 -Ba200f50/200f50NSeW -O -K -V >> $FILEPS
psbasemap -Y-10.7 -R$Regio_graf -Jx$scale_1Fig -B$Bx_tics:" $contour_text  \
	":/$By_tics:" y (km)":WeSn -V -K -O >> $FILEPS

psbasemap -R -Jx -B$Bx_tics:" $DIR_main ":N -V -K -O >> $FILEPS

cp filePalette_cpt_color2.tmp file_cpt.tmp
xyz2grd file_xyz.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
grdimage file_grd.tmp -Jx -R$Regio_graf -Cfile_cpt.tmp -O -K -V >> $FILEPS
psscale -Cfile_cpt.tmp -L -D18/1.7/8/.3 -B:" km ": -O -K -V >> $FILEPS
psxy $fcorba1 -R -M -L -Jx -W8/100/100/100 -O -K -V >> $FILEPS
grdcontour file_grd.tmp -C0.5 -A1f8 -G30/8 -W1/100 -Jx -R -O -K -V >> $FILEPS

#awk '{if($1!="#") {print $1,$2,$3 } }' $file_rot > file_zxy.tmp
rotation_min=0		### Just plot the rotations grater than rotation_min
awk '{if($1!="#" && ($3<-rotation_min || $3>rotation_min)) {print $1,$2,$3 } }' rotation_min=$rotation_min $file_rot > file_zxy.tmp
awk '{printf " %10.2f %10.2f %2i %2i %2i %2i %4i \n ", $1,$2,10,0,1,6,$3 }' file_zxy.tmp > file_Timetext.tmp

cat <<END> file_palette_cpt.tmp
-120	0	0	120	-50	0	0	120
-50	0	0	200	-40	0	0	200
-40	60	60	220	-30	60	60	220
-30	120	120	255	-20	120	120	255
-20	180	180	255	-10	180	180	255
-10	240	240	255	0	240	240	255
0	255	227	227	10	255	227	227
10	255	170	170	20	255	170	170
20	255	113	113	30	255	113	113
30	220	57	57	40	220	57	57
40	200	0	0	50	200	0	0
50	120	0	0	120	120	0	0
END
#/home1/ivone/src/Uhuru/script/make_polar_cpt.job <<END   ## Crea la paleta de color -> output: file_palette_cpt.tmp
#file_zxy.tmp
#END
psxy file_zxy.tmp -R -Bnsew -Jx -Sc0.6 -Cfile_palette_cpt.tmp -W5/255 -N -O -K -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D8/-1.5/15/.3h -B:" clockwise        Rotation [degree] > $rotation_min     counterclockwise ": -O -K -V >> $FILEPS

pstext file_Timetext.tmp -N -R -Jx -O -K -V >> $FILEPS
#pstext -R -Jx -O -N -V <<END>> $FILEPS
#$y2vel -110 12 0 0 2  $contour_text
#END

###  Rotation - time
rm file_Rotation_z.tmp, file_Rotation_xy.tmp
Punt=90
i=1
while [ $i -le $ext_res ]
do
   file_rot=$f_rotation$i
   awk '{if(NR==1) {print $3 } }' $f_rotation$i | read Time_rot
   awk '{if(NR==Punt) {print $1,$2,$3 } }' Punt=$Punt $f_rotation$i > file_zxy.tmp
   awk '{print $1,$2}' file_zxy.tmp >> file_Rotation_xy.tmp
   awk '{print Time_rot, $3}' Time_rot=$Time_rot file_zxy.tmp >> file_Rotation_z.tmp

   i=$(($i+1))
   Time_end=$Time_rot
done 
psxy file_Rotation_xy.tmp -R -Bnsew -Jx -W5/100/100/100 -O -K -V >> $FILEPS

### Negative Rotations
##psxy file_Rotation_z.tmp -X18.3 -Y7 -R0/$Time_end/-55/0 -JX8.5/-7 -Ba5f1:"time (my)":/a10f5:"rotations (degrees)":nSeW -W5/0/0/0 -O -V >> $FILEPS
### Positive Rotations
psxy file_Rotation_z.tmp -X18.3 -Y7 -R0/$Time_end/0/55 -JX8.5/7 -Ba5f1:"time (my) - Punt $Punt":/a10f5:"rotations (degrees)":nSeW -W5/0/0/0 -O -V >> $FILEPS

paste file_Rotation_z.tmp file_Rotation_xy.tmp > Rotation_time_P$Punt.xy
fi


if [ $selec -eq 22 ]		### Displacement of several points of the domain
then
FILEPS=Displacement_Points.ps
Regio_ps=$Regio_graf		#$regio_vel
Psalt=1
pstext titol.tmp -N -R$Regio_ps -Y16 -JX10/6 -N -K -V > $FILEPS
awk '{if($1!="#") {print $1,$2,$3} }' $fileres > file_xyz.tmp
##### -Y0 -Jx.002 /  -Y-0.5 -Jx.0032 ##### 
#psbasemap -Y-13 -R -Jx.015 -Ba100f50g50:" contours: elevation [km] \
#	":/a100f50g50:" ":ENWS -O -K -V >> $FILEPS

psbasemap -Y-13 -R -G255 -Jx$scale_1Fig -B$Bx_tics:"  \
	":/$By_tics:" y (km)":WESn -V -K -O >> $FILEPS



vmin=0   # Velocity field
line_jump=54
x_symbol=$((-3*$x1)) 
awk '{if($3!=0 || $4!=0){print $1,$2,(180/3.1416)*atan2($4,$3),sqrt($3*$3+$4*$4)} \
	else {print $1,$2,0,0}}' $fvelocity > file_az_vel.tmp

awk '{if($4>=vmin){print ($1,$2,$3,$4/facvel)} }' \
	facvel=$facvel vmin=$vmin file_az_vel.tmp > file_vel1.tmp
awk ' BEGIN {linia=1}
       {if(NR==linia) {print $1,$2,$3,$4; linia=linia+line_jump} }' line_jump=$line_jump file_vel1.tmp > file_vel.tmp
cat <<END>> file_vel.tmp
$x_symbol -$y1 180 2
END
tic_facvel=$(($facvel*2)) 
psxy file_vel.tmp -R -N -Jx -Sv.08/0.38/0.15n0.5 -G0 -W2/255 -K -O -V >> $FILEPS
pstext -R -Jx -O -K -N -V <<END>> $FILEPS
$x_symbol 0 12 0 0 3 $tic_facvel mm/year
#400 -200 11 0 0 0 $contour_text
END


#psbasemap -Y-10 -G255 -R -Jx.016 -Ba400/a400WS -O -K -V >> $FILEPS
xyz2grd file_xyz.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
#############   Paleta color  ############
cat <<END> file_palette_cpt.tmp
-8	0  0  100	-5	0  0  220	
-5	0  0  220	-3	0  100  220	
-3	0  100  220	-2	0  150  240	
-2	0  150  220	-1	0  200  240	
-1	0  200  240	-0.5	0  220  255	
-0.5	0  220  255	0	120  255  255	
0	75  180  155	0.100	75  200  125	
0.100	75  200  125	0.200	75  235   75	
0.200	75  235   75	0.500	125  255   75	
0.500	150  255   75	1	175  255   75	
1	200  255   75	1.5	200  200   75  
1.5	180  160  100	2	180  160  100	
2	230  210  150	3	240  220  200
3	255  255  255	4	255  255  255
END
###########################
#grdsample file_grd.tmp -Gfile_grd_sample.tmp -N101/101 -V 
file_grd=file_grd.tmp
#grdimage $file_grd -Jx -R$Regio_ps -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
#psscale -Cfile_palette_cpt.tmp -L -D22/6/10/.3 -B:." ": -O -K -V >> $FILEPS
psxy $fcorba1 -R$Regio_ps -Jx -M -L -W10/200/100/100 -O -K -V >> $FILEPS
#psxy $fcorba1 -R -Jx -Sc0.3 -G140/0/0 -W3/0 -N -O -K -V >> $FILEPS
#psxy $fcorba2 -R -Jx -Sc0.3 -G140/0/0 -W3/0 -N -O -K -V >> $FILEPS
psxy -R -Jx -W12/0 -O -K -V <<END>> $FILEPS
50 700
200 700
END
pstext -R -Jx -O -K -N -V <<END>> $FILEPS
50 730 14 0 0 0  150 km
END
wc $fcorba2 | read npunts2 a b c
nrow2=$(($npunts2-17)) 
awk '{if(NR>nrow2){print $0}}' nrow2=$nrow2 $fcorba2 > file_Pxy2.tmp
psxy file_Pxy2.tmp -R -Jx -W10/200/100/100 -O -K -V >> $FILEPS
awk '{if(NR<=nrow2){print $0}}' nrow2=$nrow2 $fcorba2 > file_Pxy1.tmp

awk '{if(NR<=3) {print $1,$2,NR} }' file_Pxy1.tmp > file_Pxy.tmp
awk '{if(NR==7) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==11) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==13) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==14) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==18) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==20) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==21) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==24) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==25) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==29) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==32) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==34) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==35) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==49) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==51) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==54) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp

#awk ' BEGIN {linia=1}
#       {if(NR==linia) {print $1,$2,NR; linia=linia+Psalt} }' Psalt=$Psalt file_Pxy1.tmp > file_Pxy.tmp

awk '{printf " %10.2f %10.2f %2i %2i %2i %2i %4i \n ", $1,$2,10,0,1,6,$3 }' file_Pxy.tmp > file_linetext.tmp
psxy file_Pxy.tmp -R -Jx -Sc0.1 -G200/100/100 -N -O -K -V >> $FILEPS
pstext file_linetext.tmp -N -R -Jx -O -K -V >> $FILEPS

awk '{if(NR==3) {print $1,$2,11,0,1,0, TIME_My } }' TIME_My=$TIME_My $fcorba2 > file_Timetext.tmp

echo ' Do you want to plot the points at different time steps? 1-Yes'
read Ptime

if [ $Ptime -eq 1 ]
then
echo ' Time step (resultats*)?'
read ext_res1
graficsth <<END
$file_results$ext_res1
END
awk '{if(NR==1) {print $4 } }' $fileres | read TIME_My
awk '{if(NR==3) {print $1,$2,11,0,1,0, TIME_My } }' TIME_My=$TIME_My $fcorba2 >> file_Timetext.tmp
psxy $fcorba1 -R -M -L -Jx -W10/80/80/250t35_20:1 -O -K -V >> $FILEPS
wc $fcorba2 | read npunts2 a b c
nrow2=$(($npunts2-17)) 
awk '{if(NR>nrow2){print $0}}' nrow2=$nrow2 $fcorba2 > file_Pxy2.tmp
psxy file_Pxy2.tmp -R -Jx -W10/80/80/250 -O -K -V >> $FILEPS
awk '{if(NR<=nrow2){print $0}}' nrow2=$nrow2 $fcorba2 > file_Pxy1.tmp

awk '{if(NR<=3) {print $1,$2,NR} }' file_Pxy1.tmp > file_Pxy.tmp
awk '{if(NR==7) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==11) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==13) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==14) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==18) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==20) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==21) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==24) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==25) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==29) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==32) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==34) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==35) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==49) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==51) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==54) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp

#awk ' BEGIN {linia=1}
#       {if(NR==linia) {print $1,$2,NR; linia=linia+Psalt} }' Psalt=$Psalt file_Pxy1.tmp > file_Pxy.tmp

awk '{printf " %10.2f %10.2f %2i %2i %2i %2i %4i \n ", $1,$2,10,0,1,6,$3 }' file_Pxy.tmp > file_linetext.tmp
psxy file_Pxy.tmp -R -Jx -Sc0.1 -G80/80/250 -N -O -K -V >> $FILEPS
pstext file_linetext.tmp -N -R -Jx -O -K -V >> $FILEPS

echo ' Time step (resultats*)?'
read ext_res2
graficsth <<END
$file_results$ext_res2
END
awk '{if(NR==1) {print $4 } }' $fileres | read TIME_My
awk '{if(NR==3) {print $1,$2,11,0,1,0, TIME_My } }' TIME_My=$TIME_My $fcorba2 >> file_Timetext.tmp
psxy $fcorba1 -R -M -L -Jx -W10/150/150/150t20_20:1 -O -K -V >> $FILEPS
wc $fcorba2 | read npunts2 a b c
nrow2=$(($npunts2-17)) 
awk '{if(NR>nrow2){print $0}}' nrow2=$nrow2 $fcorba2 > file_Pxy2.tmp
psxy file_Pxy2.tmp -R -Jx -W10/150/150/150 -O -K -V >> $FILEPS
awk '{if(NR<=nrow2){print $0}}' nrow2=$nrow2 $fcorba2 > file_Pxy1.tmp

awk '{if(NR<=3) {print $1,$2,NR} }' file_Pxy1.tmp > file_Pxy.tmp
awk '{if(NR==7) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==11) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==13) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==14) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==18) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==20) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==21) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==24) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==25) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==29) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==32) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==34) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==35) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==49) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==51) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp
awk '{if(NR==54) {print $1,$2,NR} }' file_Pxy1.tmp >> file_Pxy.tmp

#awk ' BEGIN {linia=1}
#       {if(NR==linia) {print $1,$2,NR; linia=linia+Psalt} }' Psalt=$Psalt file_Pxy1.tmp > file_Pxy.tmp

awk '{printf " %10.2f %10.2f %2i %2i %2i %2i %4i \n ", $1,$2,10,0,1,6,$3 }' file_Pxy.tmp > file_linetext.tmp
psxy file_Pxy.tmp -R -Jx -Sc0.1 -G150/150/150 -N -O -K -V >> $FILEPS
pstext file_linetext.tmp -N -R -Jx -O -K -V >> $FILEPS
fi

pstext file_Timetext.tmp -N -R -Jx -K -O -V >> $FILEPS
grdcontour $file_grd -C0.5 -G30/8 -W2/50 -Bnsew -O -Jx -R -V >> $FILEPS

rm -f file_*.tmp

fi

if [ $selec -eq 23 ]
then
file_T=$file_TK$ext_res
FILEPS=Temperature_Cross.ps
echo '  Temperature Cross Section'
pstext titol.tmp -N -R$regio_vel -X2 -Y15.3 -JX10/5 -N -K -V > $FILEPS

awk '{if (NR==1) {print $2,$3 } }' $file_T | read Time XCross
#awk '{if($1!="#") {print $1/1e3,$2/1e3,$3 }}' $file_T > file_xy.tmp
awk '{if($1!="#") {printf " \n %15.6f %15.6f %10.3f ",$1/1e3,$2/1e3,$3-273 }}' $file_T > file_xy.tmp  # Primera linea en blanc
wc file_xy.tmp | read nrow b c d
awk '{if (NR==nrow) {print $1,$2,$3 } }' nrow=$nrow file_xy.tmp | read ymax zmax Tmax
awk '{if (NR==3) {print $2 } }' file_xy.tmp | read Dz
echo ymax: $ymax, zmax: $zmax, Tmax: $Tmax
zmax=199.998	#166.665	#100.665660	#80.665860	#59.999400	#66.666000
R_cross=0/$ymax/-$Dz/$zmax
psbasemap -R$R_cross -Y-6.2 -JX25/-9.3 -B$Bx_tics:" horizontal distance (km) ":/a20f5:"depth (km)":WsEN -V -K -O >> $FILEPS
echo Dy: $y1, Dz: $Dz

### z=0 Surface (topography=0)
Px0=$XCross
Py0=0
Pxf=$XCross
Pyf=$ymax
project -C$Px0/$Py0 -E$Pxf/$Pyf -N -G$y1 -M -V > fileprojy.tmp
awk '{if($1!="#"){print $1,$2,-$3 }}' $fileres > file_xye.tmp		# elevation (surface)
 xyz2grd file_xye.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdmath file_grd.tmp NEG = fileElevation_grd.tmp
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print $3,$4 }' file_1.tmp > fileYelevation.tmp
   #psxy file_2.tmp -R$R_cross -JX -W5/0/0/0 -O -K -V >> $FILEPS
rm -f file_grd.tmp

xyz2grd file_xy.tmp -R$R_cross -H1 -Gfile_grd.tmp -I$y1/$Dz
#nearneighbor file_xy.tmp -Gfile_grd.tmp -I$y1/$Dz -R$R_cross -H1 -N2 -S1

#blockmedian file_xy.tmp -R$R_cross -I100/100 -V > file_bloc.tmp
#surface file_bloc.tmp -R$R_cross -Gfile_grd.tmp -I100/100 -V
makecpt -Crainbow -T0/1350/50 > file_palette_cpt.tmp 
cat <<END>> file_palette_cpt.tmp
1350	255 0 0	1600	255 0 0
END

#grd2cpt file_grd.tmp > file_palette_cpt.tmp
#psclip fileBC.tmp -Jt -R$regio -O -K >> $FILEPS 
grdimage file_grd.tmp -JX -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
grdcontour file_grd.tmp -JX -R -C50 -A100 -G6/8 -W1/255 -O -K -V >> $FILEPS
#psclip -R -Jt -C -O -K >> $FILEPS 
psscale -Cfile_palette_cpt.tmp -L -D13/-0.3/28/.3h -B:"           Temperature [@+o@+C]         (Time=$Time My,  x=$XCross km) ": -O -K -V >> $FILEPS
#pstext -R -JX -Bnsew -O -K -N -V <<END>> $FILEPS
#100 -50 10 0 0 1 Time=$Time My,  x=$XCross km
#END
rm -f file_*.tmp

###  Add topography, moho depth and base of the lithosphere
   #psxy fileYelevation.tmp -R$R_cross -JX -W5/0/0/0 -O -K -V >> $FILEPS

#awk '{if($1!="#"){print $1,$2,($4-$3)+$5} }' $fileres > file_xy.tmp	# moho depth
awk '{if($1!="#"){print $1,$2,$4+$5} }' $fileres > file_xy.tmp		# crust+sediment thicknesss
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_cross -JX -W6/0/0/0 -Ba500/a20g20E -K -O -V >> $FILEPS
rm -f file_*.tmp

#awk '{if($1!="#"){print ($1,$2,$6-$3) }}' $fileres > file_xy.tmp	# lithosphere depth 
awk '{if($1!="#"){print ($1,$2,$6) }}' $fileres > file_xy.tmp		# lithosphere thickness 
   xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_cross -JX -W6/0/0/0 -Bnsew -O -K -V >> $FILEPS

xyz2grd $f_resGLit -R$regio_vel -Gfile_grd.tmp -I$x1/$y1	# GLit: conductivity lithosphere-asthenosphere
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_cross -JX -W5/0/0/0 -Bnsew -O -K -V >> $FILEPS

psbasemap -Y-8 -R$regio_vel -Jx$scale_4Fig -B$Bx_tics_4/$By_tics_4:" ":WSne -O -K -V >> $FILEPS #### Plain view - elevation
cat <<END> file_palette_cpt.tmp
-8     0  0  100    -5     0  0  220	
-5     0  0  220    -3     0  100  220	
-3     0  100  220    -2     0  150  240	
-2     0  150  220    -1     0  200  240	
-1     0  200  240     -0.500     0  220  255	
-0.5     0  220  255        0   120  255  255	
0    75  180  155      0.100    75  200  125	
0.100    75  200  125      0.200    75  235   75	
0.200    75  235   75      0.500   125  255   75	
0.500   150  255   75     1   175  255   75	
1   200  255   75     1.5   200  200   75  
1.5   180  160  100     2.5   180  160  100	
2.5   230  210  150     3   240  220  200
3   255  255  255     5   255  255  255	
END
grdimage fileElevation_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D11/2.4/5.5/.25 -B:"km ": -O -K -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W3/100 -N -V >> $FILEPS
psxy -R -Jx -W5/0/0/0 -N -O -K -V <<END>> $FILEPS
$Px0 $Py0 
$Pxf $Pyf
END
pstext -R -Jx -O -N -V <<END>> $FILEPS
$Px0 $Py0  12 0 0 0 A
$Pxf $Pyf  12 0 0 0 B
END

fi

if [ $selec -eq 24 ]
then
FILEPS_HP=Heat_Production.ps
 Dx=$x1
 Dy=$y1
   awk '{if($1!="#"){print xmin+$1*Dx,ymin+$2*Dy,$3*1e6 }}' xmin=$xmin ymin=$ymin Dx=$Dx Dy=$Dy $file_HP > file_S.tmp
   awk '{if($1!="#"){print xmin+$1*Dx,ymin+$2*Dy,$4/1e3 }}' xmin=$xmin ymin=$ymin Dx=$Dx Dy=$Dy $file_HP > file_E.tmp
 
xyz2grd file_S.tmp -R$regio_vel -Gfile_grd_S.tmp -I$Dx/$Dy
xyz2grd file_E.tmp -R$regio_vel -Gfile_grd_E.tmp -I$Dx/$Dy

psbasemap -P -Y21.8 -R$regio_vel -Jx$scale_4Fig -B$Bx_tics_4/$By_tics_4:" ":WNes -K -V > $FILEPS_HP
grd2cpt file_grd_S.tmp > file_palette_cpt.tmp 
#makecpt -Cjet -T1/4/0.2 > file_palette_cpt.tmp 
grdimage file_grd_S.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS_HP
#pscoast -R -Jx -Dl -A1000/1 -Bnsew -N1 -W3 -K -O -V >> $FILEPS_HP
#grdcontour file_grd_E.tmp -Ba -C1 -A2f6 -G3/8 -O -K -Jx -R -V >> $FILEPS_HP
psscale -Cfile_palette_cpt.tmp -L -D13/2.7/5/.3 -B:"        H surface [10@+6@+ W/m @+3@+]": -O -K -V >> $FILEPS_HP

psbasemap -Y-6.8 -R -Jx -B$Bx_tics_4/$By_tics_4:" ":Wnes -K -O -V >> $FILEPS_HP
grd2cpt file_grd_E.tmp -L15/22 > file_palette_cpt.tmp 
#makecpt -Cjet -T15/23/0.5 > file_palette_cpt.tmp 
grdimage file_grd_E.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS_HP
#pscoast -R -Jx -Dl -A1000/1 -N1 -Bnsew -W3 -K -O -V >> $FILEPS_HP
psscale -Cfile_palette_cpt.tmp -L -D13/2.7/5/.3 -B:"@+ @+ H exp [km]": -O -V >> $FILEPS_HP


###  no funciona -----
## Total heat Production:   Hc=inegral(de 0 a s) H*dz = Hsup*b*[1-exp(-s/b)]	microW/m3 * km = mW/m2
#sort -k 2,2n -k 1,1n -o file_C_ord.tmp fileCrust_xy.tmp
#sort -k 2,2n -k 1,1n -o file_S_ord.tmp file_S.tmp
#sort -k 2,2n -k 1,1n -o file_E_ord.tmp file_E.tmp

#paste file_C_ord.tmp file_S_ord.tmp file_E_ord.tmp > file_cse.tmp

#awk '{if($1!="#"){print $7,$8,$6*$9*(1-exp(-$3/$9)) }}' file_cse.tmp > file_Hcrust.tmp
#xyz2grd file_Hcrust.tmp -R$regio_vel -Gfile_grd.tmp -I$Dx/$Dy

#psbasemap -Y-6.8 -R -Jx -B$Bx_tics_4/$By_tics_4:" ":Wnes -K -O -V >> $FILEPS_HP
#grd2cpt file_grd.tmp -L8/53 > file_palette_cpt.tmp 
#grdimage file_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS_HP
#pscoast -R -Jx -Dl -A1000/1 -N1 -Bnsew -W3 -K -O -V >> $FILEPS_HP
#psscale -Cfile_palette_cpt.tmp -L -D13/2.7/5/.3 -B:"        Total Heat Production [mW/m@+2@+]": -O -V >> $FILEPS_HP

rm -f file_*.tmp
fi



if [ $selec -eq 27 ]	######### Depth average vertical stress over the plate ######
then
echo "Depth-averaged vertical stress over the plate"
FILEPS=Average_VerticalStress.ps
rm -f $FILEPS

pstext titol.tmp -R$regio_vel -Y14 -X3 -JX10/6 -N -K -V > $FILEPS
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 $file_Pav$ext_res | read P0_aver
   awk '{if($1!="#"){print $1,$2,($3-P0_aver)/1e7 }}' P0_aver=$P0_aver $file_Pav$ext_res > file_xy.tmp		
##awk '{if($1!="#") {print $1,$2,$3/1e8 } }' $file_Pav$ext_res > file_xy.tmp
psbasemap -Y-10.2 -X1 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:"  \
	":/$By_tics:" y (km)":WeSn -V -K -O >> $FILEPS
xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
cat <<END> file_palette_cpt.tmp
-3.86	0	0	0	-3.31	0	0	0
-3.31	97	0	0	-2.76	97	0	0
-2.76	194	0	0	-2.21	194	0	0
-2.21	255	36	0	-1.66	255	36	0
-1.66	255	134	0	-1.11	255	134	0
-1.11	255	231	0	-0.56	255	231	0
-0.56	255	255	109	-0.01	255	255 	109
-0.01	255	255	255	0.01	255	255 	255
0.01	0	255	255	0.63	0	255	255
0.63	32	223	255	1.25	32	223	255
1.25	64	191	255	1.87	64	191	255
1.87	96	159	255	2.49	96	159	255
2.49	128	127	255	3.11	128	127	255
3.11	159	96	255	3.73	159	96	255
3.73	191	64	255	4.35	191	64	255
4.35	223	32	255	4.97	223	32	255
B	0	0	0
F	255	0	255
N	128	128	128
END
make_polar_cpt.job <<END			## Crea la paleta de color
file_xy.tmp
END
makecpt -Chaxby -T45.3/46.7/0.1   > file_palette_cpt.tmp
#makecpt -Chaxby -T-0.09/0.02/0.01   > file_palette_cpt.tmp
#grd2cpt file_grd.tmp -Chaxby > file_palette_cpt.tmp
grdimage file_grd.tmp -Jx -R -Cfile_palette_cpt.tmp -O -K -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D9.5/-1.4/22/.3h -B:" @~D@~ Depth-averaged @~s@~@-zz@- over the plate": -O -K -V >> $FILEPS
#psscale -Cfile_palette_cpt.tmp -L -D9.5/-1.4/24/.3h -B:" @~D@~ Depth-averaged @~s@~@-zz@- over the plate [x 10@+7@+ Pa]": -O -K -V >> $FILEPS
psxy $fcorba1 -R -M -L -Jx -W5/100/0/0t35_35_0_0:5 -O -K -V >> $FILEPS
#grdcontour file_grd.tmp -C0.01 -A0.02 -G2/8 -O -Bnsew -Jx -R -V >> $FILEPS

awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xy_cont.tmp	# elevation
xyz2grd file_xy_cont.tmp -R -Gfile_grd.tmp -I$x1/$y1
contour_text=" contours elevation (km)"
grdcontour file_grd.tmp -Jx -R -B$Bx_tics:" $contour_text ":Sewn -C1 -A2 -G2.5/8 -W1/0 -O -V >> $FILEPS

fi



if [ $selec -eq 28 ]	######### GPE - Gravitational Potential Energy ######
then
echo "GPE  Gravitational Potential Energy"
FILEPS=GPE.ps
rm -f $FILEPS

pstext titol.tmp -R$regio_vel -Y15 -X3 -JX10/6 -N -K -V > $FILEPS
awk '{if($1!="#"){print $1,$2,$3} }' $file_Pav$ext_res > file_xy_av-sigmazz.tmp	# (Pa)
awk '{if(NR==1){print $6} }' $file_Pav$ext_res | read ZASTH			# (km)
awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xy_elevation.tmp		# elevation(km)
sort -k 2,2n -k 1,1n -o file_sigma.tmp file_xy_av-sigmazz.tmp
sort -k 2,2n -k 1,1n -o file_elevation.tmp file_xy_elevation.tmp
paste file_sigma.tmp file_elevation.tmp > file_xy_sig_xy_ele.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_sig_xy_ele.tmp 
awk '{print $1,$2,$3*(ZASTH+$6)*1e3}' ZASTH=$ZASTH file_xy_sig_xy_ele.tmp > file_xy_Zval.tmp

awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 file_xy_Zval.tmp | read Valor0
awk '{if($1!="#"){print $1,$2,($3-Valor0)/1e9}}' Valor0=$Valor0 file_xy_Zval.tmp > file_xy.tmp		
 
psbasemap -Y-11 -X1 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:"  \
	":/$By_tics:" y (km)":WeSn -V -K -O >> $FILEPS
xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
#make_polar_cpt.job <<END			## Crea la paleta de color
#file_xy.tmp
#END
z1=1
z2=40
Dz_cpt=3
cat <<END> file_palette_cpt.tmp
0 10 10 15 $z1 10 10 15
END
makecpt -Chaxby -T$z1/$z2/$Dz_cpt >> file_palette_cpt.tmp
cat <<END>> file_palette_cpt.tmp
$z2 255 240 240 300 255 240 240
END
makecpt -Chaxby -T-2000/50/50 > file_palette_cpt.tmp
#grd2cpt file_grd.tmp > file_palette_cpt.tmp
grdimage file_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D9.5/-1.4/24/.3h \
	-B:" @~D@~ GPE [x 10@+9@+ N/m]": -O -K -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/100/0/0t35_35_0_0:5 -V >> $FILEPS
#grdcontour file_grd.tmp -C0.01 -A0.02 -G2/8 -O -Bnsew -Jx -R -V >> $FILEPS

awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xy_cont.tmp	# elevation
xyz2grd file_xy_cont.tmp -R -Gfile_grd.tmp -I$x1/$y1
contour_text=" contours elevation (km)"
grdcontour file_grd.tmp -Jx -R -B$Bx_tics:" $contour_text ":Sewn -C1 -A2 -G2.5/8 -W1/0 -O -V >> $FILEPS


fi



if [ $selec -eq 30 ]	########  Velocities at some selected points.
then
echo 
FILEPS=velocity_Points.ps
rm -f $FILEPS

#echo 'file with the points where you want to find the velocities'
#read filein_geod
filein_geod=Sites_Points.xy
file_out_VeloSites=VeloSites.out
							###   DO THAT   !!!!!!!!!!!!!!!!!!!!!!!!
vmin=0

n=$(($x2vel/$x1))
m=$(($y2vel/$y1))
sort -k 2,2n -k 1,1n -o file_velocity.tmp $fvelocity

echo $n $m $x1 $y1 > file_vE_vN.tmp
awk '{print $3,$4}' file_velocity.tmp >> file_vE_vN.tmp
awk '{print $1/1e3,$2/1e3}' $filein_geod > file_points.tmp	##  km

	###  Input file:  file_vE_vN.tmp, Points.xy
	###  Output file: $file_out_VeloSites	
velocity_Point<<END
file_vE_vN.tmp
file_points.tmp
$file_out_VeloSites
END


x_symbol=$((-3*$x1)) 
pstext titol.tmp -R$regio_vel -Y14.7 -X3.5 -JX10/5.5 -N -K -V > $FILEPS

awk '{if($3!=0 || $4!=0){ print $1,$2,(180/3.1416)*atan2($4,$3),(sqrt($3*$3+$4*$4))/facvel } \
	else {print $1,$2,0,0}}' facvel=$facvel $file_out_VeloSites > file_az_vel.tmp
cat <<END>> file_az_vel.tmp
$x_symbol -$y1 180 2
END
tic_facvel=$(($facvel*2)) 
       
psbasemap -Y-10.5 -X1 -R$regio_vel -Jx$scale_1Fig -Bwn -O -K -V >> $FILEPS

psxy file_az_vel.tmp -R -Bnsew -N -Jx -Sv.08/0.38/0.15n0.5 -G0 -W2/255 -K -O -V >> $FILEPS

pstext -R -Jx -O -N -V <<END>> $FILEPS
$x_symbol 0 11 0 0 3 $tic_facvel mm/year
END

rm -f file_*.tmp

fi





if [ $selec -eq 50 ]	    
then
FILEPS=Flexure.ps
rm -f $FILEPS

pstext titol.tmp -R$regio_vel -Y14.7 -X3 -JX10/5.5 -N -K -V > $FILEPS
awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_tisc$ext_res > file_topo.tmp
psbasemap -Y-10.7 -R$regio_vel -Jx$scale_1Fig -B$Bx_tics:"  \
	":/$By_tics:" y (km)":Wesn -V -K -O >> $FILEPS
xyz2grd file_topo.tmp -R -Gfile_grd.tmp -I$x1/$y1

cat <<END> file_palette_cpt.tmp
-8	0  0  100	-5	0  0  220	
-5	0  0  220	-3	0  100  220	
-3	0  100  220	-2	0  150  240	
-2	0  150  220	-1	0  200  240	
-1	0  200  240	-0.5	0  220  255	
-0.5	0  220  255	0	120  255  255	
0	75  180  155	0.100	75  200  125	
0.100	75  200  125	0.200	75  235   75	
0.200	75  235   75	0.500	125  255   75	
0.500	150  255   75	1	175  255   75	
1	200  255   75	1.5	200  200   75  
1.5	180  160  100	2	180  160  100	
2	230  210  150	3	240  220  200
3	255  255  255	4	255  255  255
END
#makecpt -Cgray -T1.29/3.99/0.18 > file_palette_cpt.tmp
#make_polar_cpt.job <<END   ## Crea la paleta de color
#file_topo.tmp
#END
#grd2cpt file_grd.tmp > file_palette_cpt.tmp

grdimage file_grd.tmp -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D11/-1.5/24.5/.3h -B:" elevation - flexural [km]": -O -K -V >> $FILEPS
#psxy $fcorba1 -R -O -K -N -Jx -Sc0.2 -G0 -W1/255 -N -V >> $FILEPS
#psxy $fcorba2 -R -O -K -Jx -Sc0.2 -G255 -W1/0 -N -V >> $FILEPS

awk '{if($1!="#"){print $1,$2,$3 } }' $file_tisc$ext_res > file_deflexion.tmp		# flexural deflexion (w)
xyz2grd file_deflexion.tmp -R$regio_vel -Gfile_w_grd.tmp -I$x1/$y1
contour_text="contours flexural deflection (m)"
grdcontour file_w_grd.tmp -Jx -R -B$By_tics:" $contour_text ":ewnS -C1000 -A2000 -G4/8 -W1/0 -O -V >> $FILEPS

rm -f file_*.tmp

fi

if [ $selec -eq 110 ]	    
then
FILEPS=Paramet_var.ps
rm -f $FILEPS
file_DevStr=Paramet_var.dat
x_scale=15.2
y_scale=7
scale=$x_scale/$y_scale
y_salt=-3.4

awk '{if($1!="#"){print $0 } }' $file_DevStr > file_data.tmp		# Data

RDevy=33.1/33.65		#### Localization of the Vertical Strain
RDev=0/135/$RDevy	## Elastic thickness
awk '{if($2==7 && $3==1) {print $1,$4 } }' file_data.tmp > file_xy.tmp
psbasemap -P -Y20 -R$RDev -JX$x_scale/$y_scale -Ba10f5:" Elastic thickness [km] - red ":/a0.1f0.02:" Dev Accomulated Strain":SEW -V -K > $FILEPS
psxy file_xy.tmp -R -JX -W4/250/0/0 -K -O -V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G250/0/0 -K -O -V >> $FILEPS

RDev=0/15.5/$RDevy	## Wind velocity
awk '{if($1==65 && $3==1) {print $2,$4 } }' file_data.tmp > file_xy.tmp
psbasemap -R$RDev -JX -Ba2f1:" Mean Wind velocity - blue ":/a0.1f0.1:" ":N -V -K -O >> $FILEPS
psxy file_xy.tmp -R -JX -W4/0/0/250 -O -K-V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G0/0/250 -K -O -V >> $FILEPS

awk '{if($1==65 && $2==7) {print $3,$4 } }' file_data.tmp > file_xy.tmp		## Relative humidity
psxy file_xy.tmp -R -JX -W4/0/250/0 -O -K -V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G0/250/0 -K -O -V >> $FILEPS


RDevy=0/21		#### Total Sedimentation - Erosion Volumes [km3]
RDev=0/135/$RDevy	## Elastic thickness
awk '{if($2==7 && $3==1) {print $1,$5/1e6 } }' file_data.tmp > file_xy.tmp		## Sediment
psbasemap -Y-10 -R$RDev -JX -Ba10f5:" Elastic thickness [km] ":/a5f1:" Bold - Sediment Volume [*10@+6@+ km@+3@+]":SW -V -O -K >> $FILEPS
psxy file_xy.tmp -R -JX -W4/250/0/0 -K -O -V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G250/0/0 -K -O -V >> $FILEPS

awk '{if($2==7 && $3==1) {print $1,-$6/1e6 } }' file_data.tmp > file_xy.tmp		## Erosion
psbasemap -R -JX -Ba10f5:"  ":/a5f1:" Dashed - Erosion Volume [*10@+6@+ km@+3@+]":E -V -O -K >> $FILEPS
psxy file_xy.tmp -R -JX -W4/250/0/0ta -O -K -V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G250/0/0 -O -K -V >> $FILEPS

RDev=0/15.5/$RDevy	## Wind velocity
awk '{if($1==65 && $3==1) {print $2,$5/1e6 } }' file_data.tmp > file_xy.tmp		## Sediment
psbasemap -R$RDev -JX -Ba2f1:" Mean Wind velocity - blue ":/a0.1f0.1:" ":N -V -K -O >> $FILEPS
psxy file_xy.tmp -R -JX -W4/0/0/250 -O -K- V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G0/0/250 -K -O -V >> $FILEPS
awk '{if($1==65 && $3==1) {print $2,-$6/1e6 } }' file_data.tmp > file_xy.tmp		## Erosion
psxy file_xy.tmp -R -JX -W4/0/0/250ta -O -K -V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G0/0/250 -O -K-V >> $FILEPS

## Relative humidity
awk '{if($1==65 && $2==7) {print $3,$5/1e6 } }' file_data.tmp > file_xy.tmp		## Sediment
psxy file_xy.tmp -R -JX -W4/0/250/0 -O -K- V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G0/250/0 -K -O -V >> $FILEPS
awk '{if($1==65 && $2==7) {print $3,-$6/1e6 } }' file_data.tmp > file_xy.tmp		## Erosion		
psxy file_xy.tmp -R -JX -W4/0/250/0ta -O -K -V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G0/250/0 -O -V >> $FILEPS

rm -f file_*.tmp

fi


if [ $selec -eq 111 ]	    
then
FILEPS=Paramet_var2.ps
rm -f $FILEPS
file_DevStr=Paramet_var.dat
x_scale=10
y_scale=7
scale=$x_scale/$y_scale

color_Dev=200/0/220
color_sed=220/0/0
color_eros=0/0/200

awk '{if($1!="#"){print $0 } }' $file_DevStr > file_data.tmp		# data

RTe=0/135		## Elastic thickness				#############  PANNEL 1
RDev=$RTe/33.1/33.65	## Localization of the Vertical Strain
awk '{if($2==7 && $3==1) {print $1,$4 } }' file_data.tmp > file_xy.tmp
psbasemap -P -Y20 -R$RDev -JX$x_scale/$y_scale -Ba10f5:" Elastic thickness - red ":/a0.1f0.02:" Strain localization (purple)":SNW -V -K > $FILEPS
psxy file_xy.tmp -R -JX -W4/$color_Dev -K -O -V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G$color_Dev -K -O -V >> $FILEPS

RDev=$RTe/0/19		## Sediment/erosion
psbasemap -R$RDev -JX -Ba10f5:"  ":/a2f1:" Sediment(red)/Erosion(blue) Vol. [*10@+6@+ km@+3@+]":E -V -O -K >> $FILEPS
awk '{if($2==7 && $3==1) {print $1,$5/1e6 } }' file_data.tmp > file_xy.tmp		## Sediment
psxy file_xy.tmp -R -JX -W4/$color_sed -K -O -V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G$color_sed -K -O -V >> $FILEPS
awk '{if($2==7 && $3==1) {print $1,-$6/1e6 } }' file_data.tmp > file_xy.tmp		## Erosion
psxy file_xy.tmp -R -JX -W4/$color_eros -O -K -V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G$color_eros -O -K -V >> $FILEPS

RWind=0/15.5		## Wind velocity				#############  PANNEL 2
RDev=$RWind/33.3/33.6	## Localization of the Vertical Strain
awk '{if($1==65 && $3==1) {print $2,$4 } }' file_data.tmp > file_xy.tmp
psbasemap -Y-9 -R$RDev -JX -Ba2f1:" Mean Wind velocity ":/a0.1f0.02:" Strain localization (purple)":SNW -V -K -O >> $FILEPS
psxy file_xy.tmp -R -JX -W4/$color_Dev -O -K -V >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G$color_Dev -K -O -V >> $FILEPS

RDev=$RWind/0/19	## Sediment/erosion
psbasemap -R$RDev -JX -Ba2f1:"  ":/a2f1:" Sediment(red)/Erosion(blue) Vol. [*10@+6@+ km@+3@+]":E -V -K -O >> $FILEPS
awk '{if($1==65 && $3==1) {print $2,$5/1e6 } }' file_data.tmp > file_xy.tmp		## Sediment
psxy file_xy.tmp -R -JX -W4/$color_sed -O -K >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G$color_sed -K -O >> $FILEPS
awk '{if($1==65 && $3==1) {print $2,-$6/1e6 } }' file_data.tmp > file_xy.tmp		## Erosion
psxy file_xy.tmp -R -JX -W4/$color_eros -O -K >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G$color_eros -O -K >> $FILEPS

Rhumidity=0/1.1								#############  PANNEL 3
RDev=$Rhumidity/33.3/33.6
psbasemap -Y-9 -R$RDev -JX -Ba0.1f0.05:" Relative humidity ":/a0.1f0.02:" Strain localization (purple)":SNW -V -K -O >> $FILEPS
awk '{if($1==65 && $2==7) {print $3,$4 } }' file_data.tmp > file_xy.tmp		## Relative humidity
psxy file_xy.tmp -R -JX -W4/$color_Dev -O -K >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G$color_Dev -K -O >> $FILEPS

RDev=$Rhumidity/0/19	## Sediment/erosion
psbasemap -R$RDev -JX -Ba0.1f0.05:"  ":/a2f1:" Sediment(red)/Erosion(blue) Vol. [*10@+6@+ km@+3@+]":E -V -K -O >> $FILEPS
awk '{if($1==65 && $2==7) {print $3,$5/1e6 } }' file_data.tmp > file_xy.tmp		## Sediment
psxy file_xy.tmp -R -JX -W4/$color_sed -O -K >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G$color_sed -K -O >> $FILEPS
awk '{if($1==65 && $2==7) {print $3,-$6/1e6 } }' file_data.tmp > file_xy.tmp		## Erosion		
psxy file_xy.tmp -R -JX -W4/$color_eros -O -K >> $FILEPS
psxy file_xy.tmp -R -JX -Sc0.1 -G$color_eros -O >> $FILEPS


rm -f file_*.tmp

fi



if [ $selec -eq 125 ]
then
#######   Temporal variations
FILEPS=Time_variations.ps
Gaussian=100
DIR0=MS34	
DIR1=MS93	
DIR2=MS34
DIR3=MS93			
DIR4=MS34				
DIR5=MS93	

step0=13
step1=13
step2=21
step3=21	#15
step4=25	#25
step5=25	#25
file_dranaige=res_tisc
file_erosion=res_erosion
file_results=resultats
Wcolor0=5/200/0/0ta	#4/200/130/0
Wcolor1=5/200/0/0	#4/0/150/0t25_15:1		#4/100/0/250
Wcolor2=5/0/150/0t25_15:1	#4/0/150/0
Wcolor3=5/0/150/0	#5/200/0/0ta
Wcolor4=5/0/0/200t10_15:1	#5/200/0/0
Wcolor5=5/0/0/200

echo 'choose:'
echo '1: N-S Profile '
echo '2: W-E Profile'
echo '3: Oblique Profile West'
echo '4: Oblique Profile East'
read TProfile

if [ -f $DIR0/$file_tisc$step0 ]		# elevation from tisc
then
   echo " Do you want to plot the elevation resulting from tisc? 1-Yes "
   read eflex
fi

cd $DIR0
graficsth <<END
$file_results$step0
END
awk '{print $0}' $f_limits | read x1 x2 y1 y2 x2vel y2vel 
awk '{if(NR==1) {print $4 } }' $fileres | read TIME_My
regio=$x1/$x2/$y1/$y2
regio_vel=0/$x2vel/0/$y2vel
echo "  "
echo regio = $regio
echo regio_vel = $regio_vel
cd $DIR_main
mv $DIR0/e_sedsL_Tm_vis_Q_epeff_epzz.xy graficsth0_file_tmp
f_limits_dir=$DIR0/$f_limits
mv $DIR0/corba.tmp corba0_file_tmp
fcorba0=corba0_file_tmp

cd $DIR1
graficsth <<END
$file_results$step1
END
cd $DIR_main
mv $DIR1/e_sedsL_Tm_vis_Q_epeff_epzz.xy graficsth1_file_tmp

cd $DIR2
graficsth <<END
$file_results$step2
END
cd $DIR_main
mv $DIR2/e_sedsL_Tm_vis_Q_epeff_epzz.xy graficsth2_file_tmp

cd $DIR3
graficsth <<END
$file_results$step3
END
cd $DIR_main
mv $DIR3/e_sedsL_Tm_vis_Q_epeff_epzz.xy graficsth3_file_tmp

cd $DIR4
graficsth <<END
$file_results$step4
END
cd $DIR_main
mv $DIR4/e_sedsL_Tm_vis_Q_epeff_epzz.xy graficsth4_file_tmp

cd $DIR5
graficsth <<END
$file_results$step5
END
cd $DIR_main
mv $DIR5/e_sedsL_Tm_vis_Q_epeff_epzz.xy graficsth5_file_tmp
mv $DIR5/corba.tmp corba5_file_tmp
fcorba5=corba5_file_tmp

#echo $x2vel/2 | bc -l | read xvelmig
awk '{printf "%.3f ", x2vel/2}' x2vel=$x2vel $f_limits_dir | read xvelmig
awk '{printf "%.3f ", y2vel/2}' y2vel=$y2vel $f_limits_dir | read yvelmig

if [ $TProfile -eq 1 ]		## N-S Profile
then
    Px0=2500	# $xvelmig
    Py0=1000	#0
    Pxf=2500	# $xvelmig
    Pyf=5000	# $y2vel
fi
if [ $TProfile -eq 2 ]		## W-E Profile
then
    Px0=0
    Py0=$yvelmig
    Pxf=$x2vel
    Pyf=$yvelmig
fi
if [ $TProfile -eq 3 ]		## Oblique Profile
then
    Px0=2500	#3000	#0	# Atlas	
    Py0=800	#0	#600	# Atlas	
    Pxf=500	#0	#460	# Atlas	
    Pyf=4000	#5000	#130	# Atlas	
fi
if [ $TProfile -eq 4 ]		## Oblique Profile
then
    Px0=3500	#3000	#0	# Atlas	
    Py0=800	#0	#600	# Atlas	
    Pxf=5500	#0	#460	# Atlas	
    Pyf=4000	#5000	#130	# Atlas	
fi


x_scale=15
y_scale=2.75
scale=$x_scale/$y_scale
y_salt=-3.4
#Bdist_tic=$Bx_tics_4
Bdist_tic=a500f250
Bg_tic=g250n
awk '{print sqrt((Pxf-Px0)*(Pxf-Px0)+(Pyf-Py0)*(Pyf-Py0)) }' \
	Px0=$Px0 Pxf=$Pxf Py0=$Py0 Pyf=$Pyf $f_limits_dir | read Distance
echo "Section from ($Px0,$Py0) to ($Pxf,$Pyf)"
echo Section of $Distance km

project -C$Px0/$Py0 -E$Pxf/$Pyf -N -G$y1 -V > fileprojy.tmp

op_Qef=4	## op_Qef=1 -> Surface Heat Flow	op_Qef=2 -> effective strain rate      ################### PANNEL 1
		## op_Qef=3 -> Moho Temperature		op_Qef=4 -> Accumulated Strain

if [ $op_Qef -eq 1 ]
then
    R_variable=0/$Distance/50/110 				# Surface Heat Flow $9
    psbasemap -P -Y25 -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/a10f5:" \
	 Q@-s@- (mW/m@+2@+)":WseN -V -K > $FILEPS
fi
if [ $op_Qef -eq 2 ]
then 
   R_variable=0/$Distance/-0.2/10.5				# effective strain rate $10*1e16
   psbasemap -P -Y25 -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/2f1:"  \
	@~e@~@+.@+@-eff@- (10@+-16@+ s@+-1@+)":WseN -V -K > $FILEPS
fi
if [ $op_Qef -eq 3 ] 
then
   R_variable=0/$Distance/400/570				# Temperatura Moho  $7-273 (C)
   psbasemap -P -Y25 -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/50f25:"  \
	T@-moho@- (@+o@+C)":WseN -V -K > $FILEPS
fi
if [ $op_Qef -eq 4 ]
then 
   R_variable=0/$Distance/0.9/2.7				# Accumulated Strain (adimensional)
   psbasemap -P -Y25 -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/0.5f0.25g1:"  \
	Accumulated Strain ":WseN -V -K > $FILEPS
fi


fileres_t=graficsth0_file_tmp
f_sreff=$DIR0/$file_ep_uhuru$step0
file_Strain=$DIR0/$file_TotalStrain$step0
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time 		
Time0=$Time
if [ $op_Qef -eq 1 ]
then
   awk '{if($1!="#"){print $1,$2,$9} }' $fileres_t > file_xy.tmp  	# surface heat flow 		
fi
if [ $op_Qef -eq 2 ]
then 
   #awk '{if($1!="#"){print $1,$2,$10*1e16} }' $fileres_t > file_xy.tmp   # effective strain rate		
   awk '{if($1!="#"){print $1,$2,$4*1e16} }' $f_sreff > file_xy.tmp	# strain rate used for viscosity
fi
if [ $op_Qef -eq 3 ] 							# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ] 							# Accumulated Strain	
then
   awk '{if($1!="#"){print $1/1e3,$2/1e3,$3 }}' $file_Strain > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor0 -B$Bg_tic -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth1_file_tmp
f_sreff=$DIR1/$file_ep_uhuru$step1
file_Strain=$DIR1/$file_TotalStrain$step1
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
Time1=$Time
if [ $op_Qef -eq 1 ]
then
   awk '{if($1!="#"){print $1,$2,$9 }}' $fileres_t > file_xy.tmp  # surface heat flow   		
fi
if [ $op_Qef -eq 2 ]
then 
   #awk '{if($1!="#"){print $1,$2,$10*1e16} }' $fileres_t > file_xy.tmp   # effective strain rate		
   awk '{if($1!="#"){print $1,$2,$4*1e16} }' $f_sreff > file_xy.tmp	# strain rate used for viscosity
fi
if [ $op_Qef -eq 3 ] 							# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ] 							# Accumulated Strain	
then
   awk '{if($1!="#"){print $1/1e3,$2/1e3,$3 }}' $file_Strain > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print $3,$4 }' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor1 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth2_file_tmp
f_sreff=$DIR2/$file_ep_uhuru$step2
file_Strain=$DIR2/$file_TotalStrain$step2
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
Time2=$Time
if [ $op_Qef -eq 1 ]
then
   awk '{if($1!="#"){print $1,$2,$9 }}' $fileres_t > file_xy.tmp    # surface heat flow 		
fi
if [ $op_Qef -eq 2 ]
then 
   #awk '{if($1!="#"){print $1,$2,$10*1e16} }' $fileres_t > file_xy.tmp   # effective strain rate		
   awk '{if($1!="#"){print $1,$2,$4*1e16} }' $f_sreff > file_xy.tmp	# strain rate used for viscosity
fi
if [ $op_Qef -eq 3 ] 							# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ] 							# Accumulated Strain	
then
   awk '{if($1!="#"){print $1/1e3,$2/1e3,$3 }}' $file_Strain > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print $3,$4 }' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor2 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth3_file_tmp
f_sreff=$DIR3/$file_ep_uhuru$step3
file_Strain=$DIR3/$file_TotalStrain$step3
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
Time3=$Time
if [ $op_Qef -eq 1 ]
then
   awk '{if($1!="#"){print $1,$2,$9 }}' $fileres_t > file_xy.tmp    # surface heat flow 		
fi
if [ $op_Qef -eq 2 ]
then 
   #awk '{if($1!="#"){print $1,$2,$10*1e16} }' $fileres_t > file_xy.tmp   # effective strain rate		
   awk '{if($1!="#"){print $1,$2,$4*1e16} }' $f_sreff > file_xy.tmp	# strain rate used for viscosity
fi
if [ $op_Qef -eq 3 ] 							# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ] 							# Accumulated Strain	
then
   awk '{if($1!="#"){print $1/1e3,$2/1e3,$3 }}' $file_Strain > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print $3,$4 }' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor3 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth4_file_tmp
f_sreff=$DIR4/$file_ep_uhuru$step4
file_Strain=$DIR4/$file_TotalStrain$step4
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
Time4=$Time
if [ $op_Qef -eq 1 ]
then
   awk '{if($1!="#"){print $1,$2,$9 }}' $fileres_t > file_xy.tmp    # surface heat flow 		
fi
if [ $op_Qef -eq 2 ]
then 
   #awk '{if($1!="#"){print $1,$2,$10*1e16} }' $fileres_t > file_xy.tmp   # effective strain rate		
   awk '{if($1!="#"){print $1,$2,$4*1e16} }' $f_sreff > file_xy.tmp	# strain rate used for viscosity
fi
if [ $op_Qef -eq 3 ] 							# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ] 							# Accumulated Strain	
then
   awk '{if($1!="#"){print $1/1e3,$2/1e3,$3 }}' $file_Strain > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print $3,$4 }' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor4 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth5_file_tmp
f_sreff=$DIR5/$file_ep_uhuru$step5
file_Strain=$DIR5/$file_TotalStrain$step5
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
Time5=$Time
if [ $op_Qef -eq 1 ]
then
   awk '{if($1!="#"){print $1,$2,$9 }}' $fileres_t > file_xy.tmp    # surface heat flow 		
fi
if [ $op_Qef -eq 2 ]
then 
   #awk '{if($1!="#"){print $1,$2,$10*1e16} }' $fileres_t > file_xy.tmp   # effective strain rate		
   awk '{if($1!="#"){print $1,$2,$4*1e16} }' $f_sreff > file_xy.tmp	# strain rate used for viscosity
fi
if [ $op_Qef -eq 3 ] 							# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ] 							# Accumulated Strain	
then
   awk '{if($1!="#"){print $1/1e3,$2/1e3,$3 }}' $file_Strain > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print $3,$4 }' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor5 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

pstext -R$R_variable -JX -O -K -N -V <<END>> $FILEPS
0   10		14 0 1 0 A
$Distance  10	14 0 1 0 B
END

op_Qef=3  # 1 -> T moho,  2 -> Average vertical stress,  3 -> Vertical strain rate,   4 -> GPE     	######################## PANNEL 2

if [ $op_Qef -eq 1 ] 
then
   R_variable=0/$Distance/420/1270 							# Temperatura Moho  $7-273 (C)
   psbasemap -Y$y_salt -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/a200f100:" \
	T@-moho@- (@+o@+C)":Wse -V -K -O >> $FILEPS	
fi
if [ $op_Qef -eq 2 ] 
then
#   R_variable=0/$Distance/-0.028/0.049	#-0.016/0.036	#1.778/1.83	#2/2.055	# Average vertical stress  (x 10.e9 Pa)
   R_variable=0/$Distance/-76/5		#-90/5	#-76/5					# Average vertical stress
   psbasemap -Y$y_salt -R$R_variable -JX$x_scale/-$y_scale -B$Bdist_tic:" horizontal distance (km) ":/a20f20g200:" \
	@~Ds@~@-zz@- [MPa]":Wse -V -K -O >> $FILEPS

#	(P-P@-o@-)/P@-o@- ":Wse -V -K -O >> $FILEPS
fi
if [ $op_Qef -eq 3 ]									# Vertical strain rate $11*1e16
then
   R_variable=0/$Distance/-0.5/9   # -4/5
   psbasemap -Y$y_salt -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/2f1g2:"  \
	@~e@~@+.@+@-zz@- (10@+-16@+ s@+-1@+)":Wse -V -K -O >> $FILEPS
fi
if [ $op_Qef -eq 4 ]									# GPE
then
   R_variable=0/$Distance/-3.4/9.5	#-2.5/8
   psbasemap -Y$y_salt -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/4f2g50:"  \
	@~D@~ GPE [10@+12@+ N/m]":Wse -V -K -O >> $FILEPS
fi

fileres_t=graficsth0_file_tmp
f_Paver=$DIR0/$file_Pav$step0
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ] 				# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 2 ] 				# Average vertical stress (x 10.e9 Pa)		
then
   #awk '{if($1==x1 && $2==x2){print $12 }}' x1=$x1 x2=$x2 $fileres_t | read P0_aver
   #awk '{if($1!="#"){print $1,$2,($12-P0_aver)/1e9 }}' P0_aver=$P0_aver $fileres_t > file_xy.tmp		
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 $f_Paver | read P0_aver
   awk '{if($1!="#"){print $1,$2,($3-P0_aver)/1e6 }}' P0_aver=$P0_aver $f_Paver > file_xy.tmp		
fi
if [ $op_Qef -eq 3 ]				# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ]				# GPE  
then
   awk '{if($1!="#"){print $1,$2,$3} }' $f_Paver > file_xy_av-sigmazz.tmp	# (Pa)
   awk '{if(NR==1){print $6} }' $f_Paver | read ZASTH			# (km)
   awk '{if($1!="#"){print $1,$2,$3} }' $fileres_t > file_xy_elevation.tmp	# elevation(km)
   sort -k 2,2n -k 1,1n -o file_sigma.tmp file_xy_av-sigmazz.tmp
   sort -k 2,2n -k 1,1n -o file_elevation.tmp file_xy_elevation.tmp
   paste file_sigma.tmp file_elevation.tmp > file_xy_sig_xy_ele.tmp
   awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_sig_xy_ele.tmp 
   awk '{print $1,$2,$3*(ZASTH+$6)*1e3}' ZASTH=$ZASTH file_xy_sig_xy_ele.tmp > file_xy_Zval.tmp
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 file_xy_Zval.tmp | read Valor0
   awk '{if($1!="#"){print $1,$2,($3-Valor0)/1e12}}' Valor0=$Valor0 file_xy_Zval.tmp > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor0 -B$Bg_tic -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END

rm -f file_*.tmp

fileres_t=graficsth1_file_tmp
f_Paver=$DIR1/$file_Pav$step1
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ] 				# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 2 ] 				# Average vertical stress (x 10.e9 Pa)		
then
   #awk '{if($1==x1 && $2==x2){print $12 }}' x1=$x1 x2=$x2 $fileres_t | read P0_aver
   #awk '{if($1!="#"){print $1,$2,($12-P0_aver)/1e9 }}' P0_aver=$P0_aver $fileres_t > file_xy.tmp		
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 $f_Paver | read P0_aver
   awk '{if($1!="#"){print $1,$2,($3-P0_aver)/1e6 }}' P0_aver=$P0_aver $f_Paver > file_xy.tmp	
fi
if [ $op_Qef -eq 3 ]				# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ]				# GPE  
then
   awk '{if($1!="#"){print $1,$2,$3} }' $f_Paver > file_xy_av-sigmazz.tmp	# (Pa)
   awk '{if(NR==1){print $6} }' $f_Paver | read ZASTH			# (km)
   awk '{if($1!="#"){print $1,$2,$3} }' $fileres_t > file_xy_elevation.tmp	# elevation(km)
   sort -k 2,2n -k 1,1n -o file_sigma.tmp file_xy_av-sigmazz.tmp
   sort -k 2,2n -k 1,1n -o file_elevation.tmp file_xy_elevation.tmp
   paste file_sigma.tmp file_elevation.tmp > file_xy_sig_xy_ele.tmp
   awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_sig_xy_ele.tmp 
   awk '{print $1,$2,$3*(ZASTH+$6)*1e3}' ZASTH=$ZASTH file_xy_sig_xy_ele.tmp > file_xy_Zval.tmp
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 file_xy_Zval.tmp | read Valor0
   awk '{if($1!="#"){print $1,$2,($3-Valor0)/1e12}}' Valor0=$Valor0 file_xy_Zval.tmp > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor1 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth2_file_tmp
f_Paver=$DIR2/$file_Pav$step2
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ] 				# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 2 ] 				# Average vertical stress (x 10.e9 Pa)		
then
   #awk '{if($1==x1 && $2==x2){print $12 }}' x1=$x1 x2=$x2 $fileres_t | read P0_aver
   #awk '{if($1!="#"){print $1,$2,($12-P0_aver)/1e9 }}' P0_aver=$P0_aver $fileres_t > file_xy.tmp		
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 $f_Paver | read P0_aver
   awk '{if($1!="#"){print $1,$2,($3-P0_aver)/1e6 }}' P0_aver=$P0_aver $f_Paver > file_xy.tmp		
fi
if [ $op_Qef -eq 3 ]				# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ]				# GPE  
then
   awk '{if($1!="#"){print $1,$2,$3} }' $f_Paver > file_xy_av-sigmazz.tmp	# (Pa)
   awk '{if(NR==1){print $6} }' $f_Paver | read ZASTH			# (km)
   awk '{if($1!="#"){print $1,$2,$3} }' $fileres_t > file_xy_elevation.tmp	# elevation(km)
   sort -k 2,2n -k 1,1n -o file_sigma.tmp file_xy_av-sigmazz.tmp
   sort -k 2,2n -k 1,1n -o file_elevation.tmp file_xy_elevation.tmp
   paste file_sigma.tmp file_elevation.tmp > file_xy_sig_xy_ele.tmp
   awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_sig_xy_ele.tmp 
   awk '{print $1,$2,$3*(ZASTH+$6)*1e3}' ZASTH=$ZASTH file_xy_sig_xy_ele.tmp > file_xy_Zval.tmp
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 file_xy_Zval.tmp | read Valor0
   awk '{if($1!="#"){print $1,$2,($3-Valor0)/1e12}}' Valor0=$Valor0 file_xy_Zval.tmp > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor2 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth3_file_tmp
f_Paver=$DIR3/$file_Pav$step3
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ] 				# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 2 ] 				# Average vertical stress (x 10.e9 Pa)		
then
   #awk '{if($1==x1 && $2==x2){print $12 }}' x1=$x1 x2=$x2 $fileres_t | read P0_aver
   #awk '{if($1!="#"){print $1,$2,($12-P0_aver)/1e9 }}' P0_aver=$P0_aver $fileres_t > file_xy.tmp		
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 $f_Paver | read P0_aver
   awk '{if($1!="#"){print $1,$2,($3-P0_aver)/1e6 }}' P0_aver=$P0_aver $f_Paver > file_xy.tmp		
fi
if [ $op_Qef -eq 3 ]				# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ]				# GPE  
then
   awk '{if($1!="#"){print $1,$2,$3} }' $f_Paver > file_xy_av-sigmazz.tmp	# (Pa)
   awk '{if(NR==1){print $6} }' $f_Paver | read ZASTH			# (km)
   awk '{if($1!="#"){print $1,$2,$3} }' $fileres_t > file_xy_elevation.tmp	# elevation(km)
   sort -k 2,2n -k 1,1n -o file_sigma.tmp file_xy_av-sigmazz.tmp
   sort -k 2,2n -k 1,1n -o file_elevation.tmp file_xy_elevation.tmp
   paste file_sigma.tmp file_elevation.tmp > file_xy_sig_xy_ele.tmp
   awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_sig_xy_ele.tmp 
   awk '{print $1,$2,$3*(ZASTH+$6)*1e3}' ZASTH=$ZASTH file_xy_sig_xy_ele.tmp > file_xy_Zval.tmp
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 file_xy_Zval.tmp | read Valor0
   awk '{if($1!="#"){print $1,$2,($3-Valor0)/1e12}}' Valor0=$Valor0 file_xy_Zval.tmp > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor3 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth4_file_tmp
f_Paver=$DIR4/$file_Pav$step4
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ] 				# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 2 ] 				# Average vertical stress (x 10.e9 Pa)		
then
   #awk '{if($1==x1 && $2==x2){print $12 }}' x1=$x1 x2=$x2 $fileres_t | read P0_aver
   #awk '{if($1!="#"){print $1,$2,($12-P0_aver)/1e9 }}' P0_aver=$P0_aver $fileres_t > file_xy.tmp		
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 $f_Paver | read P0_aver
   awk '{if($1!="#"){print $1,$2,($3-P0_aver)/1e6 }}' P0_aver=$P0_aver $f_Paver > file_xy.tmp		
fi
if [ $op_Qef -eq 3 ]				# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ]				# GPE  
then
   awk '{if($1!="#"){print $1,$2,$3} }' $f_Paver > file_xy_av-sigmazz.tmp	# (Pa)
   awk '{if(NR==1){print $6} }' $f_Paver | read ZASTH			# (km)
   awk '{if($1!="#"){print $1,$2,$3} }' $fileres_t > file_xy_elevation.tmp	# elevation(km)
   sort -k 2,2n -k 1,1n -o file_sigma.tmp file_xy_av-sigmazz.tmp
   sort -k 2,2n -k 1,1n -o file_elevation.tmp file_xy_elevation.tmp
   paste file_sigma.tmp file_elevation.tmp > file_xy_sig_xy_ele.tmp
   awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_sig_xy_ele.tmp 
   awk '{print $1,$2,$3*(ZASTH+$6)*1e3}' ZASTH=$ZASTH file_xy_sig_xy_ele.tmp > file_xy_Zval.tmp
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 file_xy_Zval.tmp | read Valor0
   awk '{if($1!="#"){print $1,$2,($3-Valor0)/1e12}}' Valor0=$Valor0 file_xy_Zval.tmp > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor4 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth5_file_tmp
f_Paver=$DIR5/$file_Pav$step5
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ] 				# Temperatura Moho  $7-273 (C)	
then
   awk '{if($1!="#"){print $1,$2,$7-273 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 2 ] 				# Average vertical stress (x 10.e9 Pa)		
then
   #awk '{if($1==x1 && $2==x2){print $12 }}' x1=$x1 x2=$x2 $fileres_t | read P0_aver
   #awk '{if($1!="#"){print $1,$2,($12-P0_aver)/1e9 }}' P0_aver=$P0_aver $fileres_t > file_xy.tmp		
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 $f_Paver | read P0_aver
   awk '{if($1!="#"){print $1,$2,($3-P0_aver)/1e6 }}' P0_aver=$P0_aver $f_Paver > file_xy.tmp		
fi
if [ $op_Qef -eq 3 ]				# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi
if [ $op_Qef -eq 4 ]				# GPE  
then
   awk '{if($1!="#"){print $1,$2,$3} }' $f_Paver > file_xy_av-sigmazz.tmp	# (Pa)
   awk '{if(NR==1){print $6} }' $f_Paver | read ZASTH			# (km)
   awk '{if($1!="#"){print $1,$2,$3} }' $fileres_t > file_xy_elevation.tmp	# elevation(km)
   sort -k 2,2n -k 1,1n -o file_sigma.tmp file_xy_av-sigmazz.tmp
   sort -k 2,2n -k 1,1n -o file_elevation.tmp file_xy_elevation.tmp
   paste file_sigma.tmp file_elevation.tmp > file_xy_sig_xy_ele.tmp
   awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_sig_xy_ele.tmp 
   awk '{print $1,$2,$3*(ZASTH+$6)*1e3}' ZASTH=$ZASTH file_xy_sig_xy_ele.tmp > file_xy_Zval.tmp
   awk '{if($1==x1 && $2==y1){print $3 }}' x1=$x1 y1=$y1 file_xy_Zval.tmp | read Valor0
   awk '{if($1!="#"){print $1,$2,($3-Valor0)/1e12}}' Valor0=$Valor0 file_xy_Zval.tmp > file_xy.tmp		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor5 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp


op_Qef=2  ## 1 -> Viscosity  log($8)/log(10),	2 -> Precipitation, rain [m/yr]   	######################## 	PANNEL 3

if [ $op_Qef -eq 1 ] 
then
   R_variable=0/$Distance/21.9/26 							# Viscosity log($8)/log(10)
   psbasemap -Y$y_salt -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/a1f0.5:"  \
	log (@~h@~ (Pa.s))":Wse -V -K -O >> $FILEPS	
fi
if [ $op_Qef -eq 2 ]									# Precipitation
then
   R_variable=0/$Distance/0/12
   psbasemap -Y$y_salt -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/2f1g2:"  \
	rain (m/yr)":Wse -V -K -O >> $FILEPS
fi

fileres_t=graficsth0_file_tmp
file_drai=$DIR0/$file_dranaige$step0.xyw
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ]									# Viscosity
then
   awk '{if($1!="#"){print $1,$2,log($8)/log(10) }}' $fileres_t > file_xy.tmp 
fi
if [ $op_Qef -eq 2 ]									# Precipitation
then
   awk '{print ($1,$2,$10/1e3)}' $file_drai > file_xy.tmp 
fi		
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor0 -B$Bg_tic -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth1_file_tmp
file_drai=$DIR1/$file_dranaige$step1.xyw
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ]									# Viscosity
then
   awk '{if($1!="#"){print $1,$2,log($8)/log(10) }}' $fileres_t > file_xy.tmp 
fi
if [ $op_Qef -eq 2 ]									# Precipitation
then
   awk '{print ($1,$2,$10/1e3)}' $file_drai > file_xy.tmp 
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor1 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth2_file_tmp
file_drai=$DIR2/$file_dranaige$step2.xyw
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ]									# Viscosity
then
   awk '{if($1!="#"){print $1,$2,log($8)/log(10) }}' $fileres_t > file_xy.tmp 
fi
if [ $op_Qef -eq 2 ]									# Precipitation
then
   awk '{print ($1,$2,$10/1e3)}' $file_drai > file_xy.tmp 
fi		
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor2 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth3_file_tmp
file_drai=$DIR3/$file_dranaige$step3.xyw
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ]									# Viscosity
then
   awk '{if($1!="#"){print $1,$2,log($8)/log(10) }}' $fileres_t > file_xy.tmp 
fi
if [ $op_Qef -eq 2 ]									# Precipitation
then
   awk '{print ($1,$2,$10/1e3)}' $file_drai > file_xy.tmp 
fi		
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor3 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth4_file_tmp
file_drai=$DIR4/$file_dranaige$step4.xyw
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ]									# Viscosity
then
   awk '{if($1!="#"){print $1,$2,log($8)/log(10) }}' $fileres_t > file_xy.tmp 
fi
if [ $op_Qef -eq 2 ]									# Precipitation
then
   awk '{print ($1,$2,$10/1e3)}' $file_drai > file_xy.tmp 
fi		
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor4 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth5_file_tmp
file_drai=$DIR5/$file_dranaige$step5.xyw
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_Qef -eq 1 ]									# Viscosity
then
   awk '{if($1!="#"){print $1,$2,log($8)/log(10) }}' $fileres_t > file_xy.tmp 
fi
if [ $op_Qef -eq 2 ]									# Precipitation
then
   awk '{print ($1,$2,$10/1e3)}' $file_drai > file_xy.tmp 
fi		
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor5 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

op_fig4=1	# 1:sediment + erosion,  2: sediment thickness  3: Vertical strain rate	    ######################## PANNEL 4
if [ $op_fig4 -eq 1 ]				# sediment + erosion
then
   R_variable=0/$Distance/-8/16					
   psbasemap -Y$y_salt -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/a4f2:"  \
	sedim+eros (km)":Wse -V -K -O >> $FILEPS
fi
if [ $op_fig4 -eq 2 ]				# sediment thickness $4
then
   R_variable=0/$Distance/-0.1/2.5	
   psbasemap -Y$y_salt -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/a1f0.5:"  \
	sedim.thick (km)":Wse -V -K -O >> $FILEPS
fi

if [ $op_fig4 -eq 3 ]				# Vertical strain rate $11*1e16
then
   R_variable=0/$Distance/-1/10	#-0.7/7	#-4.5/13	#-0.7/5.5	#-8.5/15.5	#-6.5/15.5
   psbasemap -Y$y_salt -Time_variations_MS34-35.psR$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km)":/1f0.5g1:"  \
	@~e@~@+.@+@-zz@-(10@+-16@+ s@+-1@+)":Wse -V -K -O >> $FILEPS
fi

fileres_t=graficsth0_file_tmp
#file_drai=$DIR0/$file_dranaige$step0.st
file_eros=$DIR0/$file_erosion$step0
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_fig4 -eq 1 ]	# sediment + erosion
then
   if [ -f $file_eros ] 
   then
      awk '{if($1!="#"){print $1/1e3,$2/1e3,$3/1e3 } }' $file_eros > file_eros1.tmp		## Erosion positive
      #awk '{if(NR>2 && $1!="#") {print $1,$2,$4/1e3 } }' $file_drai > file_eros1.tmp
      sort -k 2,2n -k 1,1n -o file_eros.tmp file_eros1.tmp
      awk '{if($1!="#") {print $1,$2,-$4 }}' $fileres_t > file_sediments1.tmp			## Sedimentation negative	
      sort -k 2,2n -k 1,1n -o file_sediments.tmp file_sediments1.tmp			
      paste file_eros.tmp file_sediments.tmp > file_eros_sed.tmp			
      awk '{printf " %10.2f %10.2f  %10.2f %10.2f \n ", $1,$2,$4,$5 }' file_eros_sed.tmp > file_eros_sed_check.tmp		
      awk '{if($1!=$3 || $2!=$4) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_eros_sed_check.tmp 
      awk '{if($6<-0.05) {print $1,$2,$6} else {print $1,$2,$3} }' file_eros_sed.tmp > file_xy.tmp	
   else
      awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
   fi
fi 
if [ $op_fig4 -eq 2 ]	# sediment thickness $4	
then
   awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
fi	
if [ $op_fig4 -eq 3 ]	# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi
	
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   #grdfilter file_grd1.tmp -D0 -Fg$Gaussian -Gfile_grd.tmp -V
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor0 -B$Bg_tic -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
rm -f file_*.tmp

fileres_t=graficsth1_file_tmp
#file_drai=$DIR1/$file_dranaige$step1.st
file_eros=$DIR1/$file_erosion$step1
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_fig4 -eq 1 ]	# sediment + erosion
then
   if [ -f $file_eros ] 
   then
      awk '{if($1!="#"){print $1/1e3,$2/1e3,$3/1e3 } }' $file_eros > file_eros1.tmp
      #awk '{if(NR>2 && $1!="#") {print $1,$2,$4/1e3 } }' $file_drai > file_eros1.tmp
      sort -k 2,2n -k 1,1n -o file_eros.tmp file_eros1.tmp
      awk '{if($1!="#") {print $1,$2,-$4 }}' $fileres_t > file_sediments1.tmp		
      sort -k 2,2n -k 1,1n -o file_sediments.tmp file_sediments1.tmp			
      paste file_eros.tmp file_sediments.tmp > file_eros_sed.tmp			
      awk '{printf " %10.2f %10.2f  %10.2f %10.2f \n ", $1,$2,$4,$5 }' file_eros_sed.tmp > file_eros_sed_check.tmp		
      awk '{if($1!=$3 || $2!=$4) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_eros_sed_check.tmp 
      awk '{if($6<-0.05) {print $1,$2,$6} else {print $1,$2,$3} }' file_eros_sed.tmp > file_xy.tmp	
   else
      awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
   fi
fi 
if [ $op_fig4 -eq 2 ]	# sediment thickness $4	
then
   awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
fi	
if [ $op_fig4 -eq 3 ]	# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi
	
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   #grdfilter file_grd1.tmp -D0 -Fg$Gaussian -Gfile_grd.tmp -V
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor1 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
rm -f file_*.tmp

fileres_t=graficsth2_file_tmp
#file_drai=$DIR2/$file_dranaige$step2.st
file_eros=$DIR2/$file_erosion$step2
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_fig4 -eq 1 ]	# sediment + erosion
then
   if [ -f $file_eros ] 
   then
      awk '{if($1!="#"){print $1/1e3,$2/1e3,$3/1e3 } }' $file_eros > file_eros1.tmp
      #awk '{if(NR>2 && $1!="#") {print $1,$2,$4/1e3 } }' $file_drai > file_eros1.tmp
      sort -k 2,2n -k 1,1n -o file_eros.tmp file_eros1.tmp
      awk '{if($1!="#") {print $1,$2,-$4 }}' $fileres_t > file_sediments1.tmp		
      sort -k 2,2n -k 1,1n -o file_sediments.tmp file_sediments1.tmp			
      paste file_eros.tmp file_sediments.tmp > file_eros_sed.tmp			
      awk '{printf " %10.2f %10.2f  %10.2f %10.2f \n ", $1,$2,$4,$5 }' file_eros_sed.tmp > file_eros_sed_check.tmp		
      awk '{if($1!=$3 || $2!=$4) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_eros_sed_check.tmp 
      awk '{if($6<-0.05) {print $1,$2,$6} else {print $1,$2,$3} }' file_eros_sed.tmp > file_xy.tmp	
   else
      awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
   fi
fi 
if [ $op_fig4 -eq 2 ]	# sediment thickness $4	
then
   awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
fi	
if [ $op_fig4 -eq 3 ]	# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi	
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   #grdfilter file_grd1.tmp -D0 -Fg$Gaussian -Gfile_grd.tmp -V
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor2 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
rm -f file_*.tmp

fileres_t=graficsth3_file_tmp
#file_drai=$DIR3/$file_dranaige$step3.st
file_eros=$DIR3/$file_erosion$step3
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_fig4 -eq 1 ]	# sediment + erosion
then
   if [ -f $file_eros ] 
   then
      awk '{if($1!="#"){print $1/1e3,$2/1e3,$3/1e3} }' $file_eros > file_eros1.tmp
      #awk '{if(NR>2 && $1!="#") {print $1,$2,$4/1e3 } }' $file_drai > file_eros1.tmp
      sort -k 2,2n -k 1,1n -o file_eros.tmp file_eros1.tmp
      awk '{if($1!="#") {print $1,$2,-$4 }}' $fileres_t > file_sediments1.tmp		
      sort -k 2,2n -k 1,1n -o file_sediments.tmp file_sediments1.tmp			
      paste file_eros.tmp file_sediments.tmp > file_eros_sed.tmp			
      awk '{printf " %10.2f %10.2f  %10.2f %10.2f \n ", $1,$2,$4,$5 }' file_eros_sed.tmp > file_eros_sed_check.tmp		
      awk '{if($1!=$3 || $2!=$4) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_eros_sed_check.tmp 
      awk '{if($6<-0.05) {print $1,$2,$6} else {print $1,$2,$3} }' file_eros_sed.tmp > file_xy.tmp	
   else
      awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
   fi
fi 
if [ $op_fig4 -eq 2 ]	# sediment thickness $4	
then
   awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
fi	
if [ $op_fig4 -eq 3 ]	# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi	
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   #grdfilter file_grd1.tmp -D0 -Fg$Gaussian -Gfile_grd.tmp -V
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor3 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
rm -f file_*.tmp

fileres_t=graficsth4_file_tmp
#file_drai=$DIR4/$file_dranaige$step4.st
file_eros=$DIR4/$file_erosion$step4
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_fig4 -eq 1 ]	# sediment + erosion
then
   if [ -f $file_eros ] 
   then
      awk '{if($1!="#"){print $1/1e3,$2/1e3,$3/1e3} }' $file_eros > file_eros1.tmp
      #awk '{if(NR>2 && $1!="#") {print $1,$2,$4/1e3 } }' $file_drai > file_eros1.tmp
      sort -k 2,2n -k 1,1n -o file_eros.tmp file_eros1.tmp
      awk '{if($1!="#") {print $1,$2,-$4 }}' $fileres_t > file_sediments1.tmp		
      sort -k 2,2n -k 1,1n -o file_sediments.tmp file_sediments1.tmp			
      paste file_eros.tmp file_sediments.tmp > file_eros_sed.tmp			
      awk '{printf " %10.2f %10.2f  %10.2f %10.2f \n ", $1,$2,$4,$5 }' file_eros_sed.tmp > file_eros_sed_check.tmp		
      awk '{if($1!=$3 || $2!=$4) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_eros_sed_check.tmp 
      awk '{if($6<-0.05) {print $1,$2,$6} else {print $1,$2,$3} }' file_eros_sed.tmp > file_xy.tmp	
   else
      awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
   fi
fi 
if [ $op_fig4 -eq 2 ]	# sediment thickness $4	
then
   awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
fi	
if [ $op_fig4 -eq 3 ]	# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi	
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   #grdfilter file_grd1.tmp -D0 -Fg$Gaussian -Gfile_grd.tmp -V
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor4 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
rm -f file_*.tmp

fileres_t=graficsth5_file_tmp
#file_drai=$DIR5/$file_dranaige$step5.st
file_eros=$DIR5/$file_erosion$step5
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_fig4 -eq 1 ]	# sediment + erosion
then
   if [ -f $file_eros ] 
   then
      awk '{if($1!="#"){print $1/1e3,$2/1e3,$3/1e3} }' $file_eros > file_eros1.tmp
      #awk '{if(NR>2 && $1!="#") {print $1,$2,$4/1e3 } }' $file_drai > file_eros1.tmp
      sort -k 2,2n -k 1,1n -o file_eros.tmp file_eros1.tmp
      awk '{if($1!="#") {print $1,$2,-$4 }}' $fileres_t > file_sediments1.tmp		
      sort -k 2,2n -k 1,1n -o file_sediments.tmp file_sediments1.tmp			
      paste file_eros.tmp file_sediments.tmp > file_eros_sed.tmp			
      awk '{printf " %10.2f %10.2f  %10.2f %10.2f \n ", $1,$2,$4,$5 }' file_eros_sed.tmp > file_eros_sed_check.tmp		
      awk '{if($1!=$3 || $2!=$4) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_eros_sed_check.tmp 
      awk '{if($6<-0.05) {print $1,$2,$6} else {print $1,$2,$3} }' file_eros_sed.tmp > file_xy.tmp	
   else
      awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
   fi
fi 
if [ $op_fig4 -eq 2 ]	# sediment thickness $4	
then
   awk '{if($1!="#"){print $1,$2,$4 }}' $fileres_t > file_xy.tmp
fi	
if [ $op_fig4 -eq 3 ]	# Vertical strain rate $11*1e16  
then
   awk '{if($1!="#"){print $1,$2,$11*1e16 }}' $fileres_t > file_xy.tmp		
fi	
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   #grdfilter file_grd1.tmp -D0 -Fg$Gaussian -Gfile_grd.tmp -V
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor5 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
rm -f file_*.tmp



R_variable=0/$Distance/-3.5/11					# Elevation $3	    ######################## PANNEL 5

fileres_t=graficsth0_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
file_topo_tisc=$DIR0/$file_tisc$step0
if [ $eflex -eq 1 ]
then   
   awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_topo_tisc > file_topo.tmp
   sort -k 2,2n -k 1,1n -o file_xy.tmp file_topo.tmp
   text_e="e - tisc (km)"
 else  
   awk '{if($1!="#"){print $1,$2,$3 }}' $fileres_t > file_xy.tmp   		
   text_e="e (km)"
fi

psbasemap -Y$y_salt -R$R_variable -JX$x_scale/$y_scale -B$Bdist_tic:" horizontal distance (km) ":/a2f1g2:" $text_e ":Wse -V -K -O >> $FILEPS

xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor0 -B$Bg_tic -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth1_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
file_topo_tisc=$DIR1/$file_tisc$step1
if [ $eflex -eq 1 ]
then   
   awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_topo_tisc > file_topo.tmp
   sort -k 2,2n -k 1,1n -o file_xy.tmp file_topo.tmp
 else  
   awk '{if($1!="#"){print $1,$2,$3 }}' $fileres_t > file_xy.tmp   		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor1 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth2_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
file_topo_tisc=$DIR2/$file_tisc$step2
if [ $eflex -eq 1 ]
then   
   awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_topo_tisc > file_topo.tmp
   sort -k 2,2n -k 1,1n -o file_xy.tmp file_topo.tmp
 else  
   awk '{if($1!="#"){print $1,$2,$3 }}' $fileres_t > file_xy.tmp   		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor2 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth3_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
file_topo_tisc=$DIR3/$file_tisc$step3
if [ $eflex -eq 1 ]
then   
   awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_topo_tisc > file_topo.tmp
   sort -k 2,2n -k 1,1n -o file_xy.tmp file_topo.tmp
 else  
   awk '{if($1!="#"){print $1,$2,$3 }}' $fileres_t > file_xy.tmp   		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor3 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth4_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
file_topo_tisc=$DIR4/$file_tisc$step4
if [ $eflex -eq 1 ]
then   
   awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_topo_tisc > file_topo.tmp
   sort -k 2,2n -k 1,1n -o file_xy.tmp file_topo.tmp
 else  
   awk '{if($1!="#"){print $1,$2,$3 }}' $fileres_t > file_xy.tmp   		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor4 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth5_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
file_topo_tisc=$DIR5/$file_tisc$step5
if [ $eflex -eq 1 ]
then   
   awk '{if($1!="#") {print $1,$2,$5/1e3 } }' $file_topo_tisc > file_topo.tmp
   sort -k 2,2n -k 1,1n -o file_xy.tmp file_topo.tmp
 else  
   awk '{if($1!="#"){print $1,$2,$3 }}' $fileres_t > file_xy.tmp   		
fi
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor5 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp


R_variable=0/$Distance/30/85	#33/74	#33/85				# Crustal thickness $5	    ######################## PANNEL 6
psbasemap -Y$y_salt -R$R_variable -JX$x_scale/-$y_scale -B$Bdist_tic:" horizontal distance (km) ":/a10f2g10:" \
	s (km)":Wse -V -K -O >> $FILEPS

fileres_t=graficsth0_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
awk '{if($1!="#"){print $1,$2,$5 }}' $fileres_t > file_xy.tmp   		
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor0 -B$Bg_tic -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth1_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
awk '{if($1!="#"){print $1,$2,$5 }}' $fileres_t > file_xy.tmp   		
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor1 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth2_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
awk '{if($1!="#"){print $1,$2,$5 }}' $fileres_t > file_xy.tmp   		
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor2 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth3_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
awk '{if($1!="#"){print $1,$2,$5 }}' $fileres_t > file_xy.tmp   		
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor3 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth4_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
awk '{if($1!="#"){print $1,$2,$5 }}' $fileres_t > file_xy.tmp   		
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor4 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth5_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
awk '{if($1!="#"){print $1,$2,$5 }}' $fileres_t > file_xy.tmp   		
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor5 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp


op_lit=1	# op_lit=1 -> lithospheric mantle thickness $6-$4-$5 	op_lit!=1 -> lithosphere depth $6-$3	    ######################## PANNEL 7
if [ $op_lit -eq 1 ]	# lithospheric mantle thickness $6-$4-$5
then
   R_variable=0/$Distance/80/220	#80/130	#0/170	#87/200		#87/205
   ylabel="h@-m@- (km)"	
else			# lithosphere tdepth $6-$3
   R_variable=0/$Distance/125/155
   ylabel="depth lithosp. (km)"
fi		 
psbasemap -Y$y_salt -R$R_variable -JX$x_scale/-$y_scale -B$Bdist_tic:" horizontal distance (km) ":/a30f10g30:"  \
	$ylabel ":Wse -V -K -O >> $FILEPS

fileres_t=graficsth0_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_lit -eq 1 ]	# lithospheric mantle thickness $6-$4-$5
then
   awk '{if($1!="#"){print $1,$2,$6-$4-$5 }}' $fileres_t > file_xy.tmp  
else			# lithospheric thickness $6	
   awk '{if($1!="#"){print $1,$2,$6-$3 }}' $fileres_t > file_xy.tmp   
fi			
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor0 -B$Bg_tic -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth1_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_lit -eq 1 ]	# lithospheric mantle thickness $6-$4-$5
then
   awk '{if($1!="#"){print $1,$2,$6-$4-$5 }}' $fileres_t > file_xy.tmp  
else			# lithospheric thickness $6	
   awk '{if($1!="#"){print $1,$2,$6-$3 }}' $fileres_t > file_xy.tmp   
fi			
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor1 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth2_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_lit -eq 1 ]	# lithospheric mantle thickness $6-$4-$5
then
   awk '{if($1!="#"){print $1,$2,$6-$4-$5 }}' $fileres_t > file_xy.tmp  
else			# lithospheric thickness $6	
   awk '{if($1!="#"){print $1,$2,$6-$3 }}' $fileres_t > file_xy.tmp   
fi			
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor2 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth3_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_lit -eq 1 ]	# lithospheric mantle thickness $6-$4-$5
then
   awk '{if($1!="#"){print $1,$2,$6-$4-$5 }}' $fileres_t > file_xy.tmp  
else			# lithospheric thickness $6	
   awk '{if($1!="#"){print $1,$2,$6-$3 }}' $fileres_t > file_xy.tmp   
fi			
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor3 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth4_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_lit -eq 1 ]	# lithospheric mantle thickness $6-$4-$5
then
   awk '{if($1!="#"){print $1,$2,$6-$4-$5 }}' $fileres_t > file_xy.tmp  
else			# lithospheric thickness $6	
   awk '{if($1!="#"){print $1,$2,$6-$3 }}' $fileres_t > file_xy.tmp   
fi			
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor4 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp

fileres_t=graficsth5_file_tmp
awk '{if(NR==1){print $4,$5 }}' $fileres_t | read Time   		
if [ $op_lit -eq 1 ]	# lithospheric mantle thickness $6-$4-$5
then
   awk '{if($1!="#"){print $1,$2,$6-$4-$5 }}' $fileres_t > file_xy.tmp  
else			# lithospheric thickness $6	
   awk '{if($1!="#"){print $1,$2,$6-$3 }}' $fileres_t > file_xy.tmp   
fi			
xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
   grdtrack fileprojy.tmp -Gfile_grd.tmp > file_1.tmp
   awk '{print($3,$4)}' file_1.tmp > file_2.tmp
   psxy file_2.tmp -R$R_variable -JX -W$Wcolor5 -K -O -V >> $FILEPS
wc file_2.tmp | read rows a b c
awk '{if(NR==rows) {print $1,$2} }' rows=$rows file_2.tmp | read xt_file yt_file
#pstext -R$R_variable -JX -K -O -N -V <<END>> $FILEPS
#$xt_file  $yt_file	6 0 0 1 $Time
#END
rm -f file_*.tmp


## Plain view
Scale_pv=$(($scale_4Fig/2)) 
psbasemap -Y-3.8 -X-1.2 -R$regio_vel -Jx$Scale_pv -Bnsew -O -K -V >> $FILEPS
psxy $fcorba5 -R -M -L -Jx -W$Wcolor5 -O -K -V >> $FILEPS
psxy $fcorba0 -R -M -L -Jx -W$Wcolor0 -O -K -V >> $FILEPS
psxy -R$regio_vel -Jx -W3/0/0/0 -N -O -K -V <<END>> $FILEPS
$Px0 $Py0 
$Pxf $Pyf
END
if [ -f TITOL.tmp ] 
then
    awk '{print 1900,1400-NR*150,8,0,0,0,$0 }' TITOL.tmp > file_text.tmp
else
    echo  7000 $y2vel 8 0 0 0 $DIR_main > file_text.tmp
fi
pstext file_text.tmp -R -Jx -N -K -O -V >> $FILEPS
pstext -R -Jx -O -K -N -V <<END>> $FILEPS
$Px0 $Py0  8 0 0 0 A ($Px0, $Py0)
$Pxf $Pyf  8 0 0 0 B ($Pxf, $Pyf)
END
pstext -X5 -R-1/10/0/6 -Jx0.44 -O -K -N -V <<END>> $FILEPS
3.3 5  8 0 0 0 $DIR0 : $Time0
3.3 4  8 0 0 0 $DIR1 : $Time1
3.3 3  8 0 0 0 $DIR2 : $Time2
3.3 2  8 0 0 0 $DIR3 : $Time3
3.3 1  8 0 0 0 $DIR4 : $Time4
3.3 0  8 0 0 0 $DIR5 : $Time5
END
psxy -R -Jx -W$Wcolor0 -O -K -V <<END>> $FILEPS
0 5
3 5
END
psxy -R -Jx -W$Wcolor1 -O -K -V <<END>> $FILEPS
0 4
3 4
END
psxy -R -Jx -W$Wcolor2 -O -K -V <<END>> $FILEPS
0 3
3 3
END
psxy -R -Jx -W$Wcolor3 -O -K -V <<END>> $FILEPS
0 2
3 2
END
psxy -R -Jx -W$Wcolor4 -O -K -V <<END>> $FILEPS
0 1
3 1
END
psxy -R -Jx -W$Wcolor5 -O -V <<END>> $FILEPS
0 0
3 0
END

rm -f *file_tmp
cd $DIR0
rm -f titol.tmp TITOL.tmp Points.tmp
rm -f $fileres $fepunt12 $fprin_stress $fvelocity $f_limits $f_resGLit $fcorba1
cd $DIR_main
cd $DIR1
rm -f titol.tmp TITOL.tmp Points.tmp
rm -f $fileres $fepunt12 $fprin_stress $fvelocity $f_limits $f_resGLit $fcorba1
cd $DIR_main
cd $DIR2
rm -f titol.tmp TITOL.tmp Points.tmp
rm -f $fileres $fepunt12 $fprin_stress $fvelocity $f_limits $f_resGLit $fcorba1
cd $DIR_main
cd $DIR3
rm -f titol.tmp TITOL.tmp Points.tmp
rm -f $fileres $fepunt12 $fprin_stress $fvelocity $f_limits $f_resGLit $fcorba1
cd $DIR_main
cd $DIR4
rm -f titol.tmp TITOL.tmp Points.tmp
rm -f $fileres $fepunt12 $fprin_stress $fvelocity $f_limits $f_resGLit $fcorba1
cd $DIR_main
cd $DIR5
rm -f titol.tmp TITOL.tmp Points.tmp
rm -f $fileres $fepunt12 $fprin_stress $fvelocity $f_limits $f_resGLit $fcorba1
cd $DIR_main


fi

if [ $selec -eq 126 ]     #######################################################################################  
then
step0=17
step1=18
step2=19
step3=20
step4=21
step5=22
step6=23
step7=24
step8=25
step9=
step10=
step11=
step12=
step13=
step14=
step15=
step16=

file_results=resultats		# input file
file_graf=graficsth_file_tmp	# output file from graficsth

graficsth <<END
$file_results$step0
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step0
awk '{print $0}' $f_limits | read x1 x2 y1 y2 x2vel y2vel 
regio_vel=0/$x2vel/0/$y2vel

if [ -f  $file_results$step1 ] 
then
graficsth <<END
$file_results$step1
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step1
fi

if [ -f  $file_results$step2 ] 
then
graficsth <<END
$file_results$step2
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step2
fi

if [ -f  $file_results$step3 ] 
then
graficsth <<END
$file_results$step3
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step3
fi

if [ -f  $file_results$step4 ] 
then
graficsth <<END
$file_results$step4
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step4
fi

if [ -f  $file_results$step5 ] 
then
graficsth <<END
$file_results$step5
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step5
fi

if [ -f  $file_results$step6 ] 
then
graficsth <<END
$file_results$step6
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step6
fi

if [ -f  $file_results$step7 ] 
then
graficsth <<END
$file_results$step7
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step7
fi

if [ -f  $file_results$step8 ] 
then
graficsth <<END
$file_results$step8
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step8
fi

if [ -f  $file_results$step9 ] 
then
graficsth <<END
$file_results$step9
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step9
fi

if [ -f  $file_results$step10 ] 
then
graficsth <<END
$file_results$step10
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step10
fi

if [ -f  $file_results$step11 ] 
then
graficsth <<END
$file_results$step11
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step11
fi

if [ -f  $file_results$step12 ] 
then
graficsth <<END
$file_results$step12
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step12
fi

if [ -f  $file_results$step13 ] 
then
graficsth <<END
$file_results$step13
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step13
fi

if [ -f  $file_results$step14 ] 
then
graficsth <<END
$file_results$step14
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step14
fi

if [ -f  $file_results$step15 ] 
then
graficsth <<END
$file_results$step15
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step15
fi

if [ -f  $file_results$step16 ] 
then
graficsth <<END
$file_results$step16
END
mv e_sedsL_Tm_vis_Q_epeff_epzz.xy $file_graf$step16
fi

FILEPS=variable_time.ps
echo ' .... working ....'

fileres_t=$file_graf$step0
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END

fileres_t=$file_graf$step1
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step2
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step3
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step4
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step5
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step6
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step7
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step8
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step9
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step10
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step11
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step12
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step13
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step14
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step15
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi

fileres_t=$file_graf$step16
if [ -f  $fileres_t ] 
then
maxmin_uhuru.ksh <<END
$fileres_t
$regio_vel
$x1
$y1
END
fi
scale=15/3.2
y_salt=-4.5


wc file_time_emax.tmp | read nrow a b c 
awk '{if(NR==nrow){print $1 }}' nrow=$nrow file_time_emax.tmp | read Time 
echo ' Time ='	$Time
	
    ######  elevation ######
R_t_var=0/$Time/-2.5/3.7				# elevation
psbasemap -P -Y24.5 -R$R_t_var -JX$scale -Ba5f2.5:" Time (Myear) ":/1f0.5:"  \
	elevation (km)":WseN -V -K > $FILEPS
psxy file_time_emax.tmp -R -JX -Sc0.1 -G0 -N -K -O -V >> $FILEPS
psxy file_time_emax.tmp -R -JX -W4/0 -N -K -O -V >> $FILEPS
awk '{printf " %8.4f %8.4f %3i %3i %3i %3i (%5.1f,%5.1f)\n",$1,$2,6,90,0,0,$3,$4 }' \
	file_time_emax.tmp > file_text_xyemax.tmp 
pstext file_text_xyemax.tmp -R -JX -N -K -O -V >> $FILEPS
psxy file_time_emin.tmp -R -JX -St0.3 -G0 -N -K -O -V >> $FILEPS
psxy file_time_emin.tmp -R -JX -W4/0 -N -K -O -V >> $FILEPS
awk '{printf " %8.4f %8.4f %3i %3i %3i %3i (%5.1f,%5.1f)\n",$1,$2,6,90,0,3,$3,$4 }' \
	file_time_emin.tmp > file_text_xyemin.tmp 
pstext file_text_xyemin.tmp -R -JX -N -K -O -V >> $FILEPS

awk '{print 0,7-NR*0.6,8,0,0,0,$0 }' TITOL.tmp > file_text.tmp
pstext file_text.tmp -R -JX -N -K -O -V >> $FILEPS

Rgrade=0/$Time/4/12				# maxima elevation - minima elevation
psbasemap -Y$y_salt -R$Rgrade -JX -Ba5f2.5:"  ":/a1f0.5:" \
	@~D@~e (km)":Wsen -V -K -O >> $FILEPS
paste file_time_emax.tmp file_time_emin.tmp > file_time_e.tmp 
awk '{printf " %8.4f %8.4f %3i %3i %3i %3i De=%5.3f\n",$1,$2-$6,7,0,0,2,$2-$6 }' \
	file_time_e.tmp > file_text_e.tmp 
psxy file_text_e.tmp -R -JX -W4/0 -N -O -K -V >> $FILEPS
pstext file_text_e.tmp -R -JX -N -K -O -V >> $FILEPS


    ######  lithospheric mantle ######
R_t_var=0/$Time/40/120				# lithospheric mantle
psbasemap -Y$y_salt -R$R_t_var -JX -Ba5f2.5:"  ":/20f10:"  \
	lit. mantle (km)":Wsen -V -K -O >> $FILEPS
psxy file_time_lmmax.tmp -R -JX -Sc0.1 -G0 -N -K -O -V >> $FILEPS
psxy file_time_lmmax.tmp -R -JX -W4/0 -N -K -O -V >> $FILEPS
awk '{printf " %8.4f %8.4f %3i %3i %3i %3i (%5.1f,%5.1f)\n",$1,$2,6,90,0,0,$3,$4 }' \
	file_time_lmmax.tmp > file_text_xylmmax.tmp 
pstext file_text_xylmmax.tmp -R -JX -N -K -O -V >> $FILEPS
psxy file_time_lmmin.tmp -R -JX -St0.3 -G0 -N -K -O -V >> $FILEPS
psxy file_time_lmmin.tmp -R -JX -W4/0 -N -K -O -V >> $FILEPS
awk '{printf " %8.4f %8.4f %3i %3i %3i %3i (%5.1f,%5.1f)\n",$1,$2,6,90,0,3,$3,$4 }' \
	file_time_lmmin.tmp > file_text_xylmmin.tmp 
pstext file_text_xylmmin.tmp -R -JX -N -K -O -V >> $FILEPS

Rgrade=0/$Time/10/60				# maxim lithospheric mantle - minim lithospheric mantle
psbasemap -Y$y_salt -R$Rgrade -JX -Ba5f2.5:"  ":/a10f5:" \
	@~D@~lit.mantle (km)":Wsen -V -K -O >> $FILEPS
paste file_time_lmmax.tmp file_time_lmmin.tmp > file_time_lm.tmp 
awk '{printf " %8.4f %8.4f %3i %3i %3i %3i Dlm=%5.3f\n",$1,$2-$6,7,0,0,2,$2-$6 }' \
	file_time_lm.tmp > file_text_lm.tmp 
psxy file_text_lm.tmp -R -JX -W4/0 -N -O -K -V >> $FILEPS
pstext file_text_lm.tmp -R -JX -N -K -O -V >> $FILEPS


    ######  crustal thicknesss ######
R_t_var=0/$Time/15/56				# crustal thickness
psbasemap -Y$y_salt -R$R_t_var -JX -Ba5f2.5:"  ":/a5f2.5:" \
	crust.thick (km)":Wsen -V -K -O >> $FILEPS
psxy file_time_smax.tmp -R -JX -Sc0.1 -G0 -N -K -O -V >> $FILEPS
psxy file_time_smax.tmp -R -JX -W4/0 -N -O -K -V >> $FILEPS
awk '{printf " %8.4f %8.4f %3i %3i %3i %3i (%5.1f,%5.1f)\n",$1,$2,6,90,0,0,$3,$4 }' \
	file_time_smax.tmp > file_text_xysmax.tmp 
pstext file_text_xysmax.tmp -R -JX -N -K -O -V >> $FILEPS
psxy file_time_smin.tmp -R -JX -St0.3 -G0 -N -K -O -V >> $FILEPS
psxy file_time_smin.tmp -R -JX -W4/0 -N -O -K -V >> $FILEPS
awk '{printf " %8.4f %8.4f %3i %3i %3i %3i (%5.1f,%5.1f)\n",$1,$2,6,90,0,3,$3,$4 }' \
	file_time_smin.tmp > file_text_xysmin.tmp 
pstext file_text_xysmin.tmp -R -JX -N -K -O -V >> $FILEPS

Rgrads=0/$Time/26/58				# maxima crustal thickness - minima crustal thickness
psbasemap -Y$y_salt -R$Rgrads -JX -Ba5f2.5:" Time (Myear) ":/a4f2:" \
	@~D@~s (km)":WSen -V -K -O >> $FILEPS

paste file_time_smax.tmp file_time_smin.tmp > file_time_s.tmp 
awk '{printf " %8.4f %8.4f %3i %3i %3i %3i Ds=%5.3f\n",$1,$2-$6,7,0,0,2,$2-$6 }' \
	file_time_s.tmp > file_text_s.tmp 
psxy file_text_s.tmp -R -JX -W4/0 -N -O -K -V >> $FILEPS
pstext file_text_s.tmp -R -JX -N -O -V >> $FILEPS

    ######  vertical strain rate ######  Create the file, it is not include in the figure
paste file_time_ezzmax.tmp file_time_ezzmin.tmp > file_time_ezz.tmp 



if [ -f elevation.t ]
then
   echo '   '
   echo ' The files elevation.t lithospheric_mantle.t crust.t already exist. '
   echo '   1- I want to remove the old files '
   echo '   2- I want to add this results to the existing files '
   read opt_file
else
   opt_file=1
fi

if [ $opt_file -eq 1 ] 
then
   echo '# Elevation: Time, e max, x emax, y emax, e min, x emin, y emin' > elevation.t 
   echo '# Lith.mantle: Time, lm max, x lmmax, y lmmax, lm min, x lmmin, y lmmin' > lithospheric_mantle.t 
   echo '# Crust: Time, s max, x smax, y smax, s min, x smin, y smin' > crust.t 
   echo '# Vertical Strain Rate: Time, ezz max, x ezzmax, y ezzmax, ezz min, x ezzmin, y ezzmin' > VerticalStrainRate.t
fi

awk '{print $1,$2,$3,$4,$6,$7,$8 }' file_time_e.tmp >> elevation.t
awk '{print $1,$2,$3,$4,$6,$7,$8 }' file_time_lm.tmp >> lithospheric_mantle.t
awk '{print $1,$2,$3,$4,$6,$7,$8 }' file_time_s.tmp >> crust.t
awk '{print $1,$2,$3,$4,$6,$7,$8 }' file_time_ezz.tmp >> VerticalStrainRate.t

rm -f graficsth_file_tmp*

fi


if [ $selec -eq 130 ]    ####################  DIFFERENCES #############3
then
FILEPS=Difference.ps

ls --color=auto
echo " Directory with the Model 1?"
read DIR1
echo " Extension to be plotted File with the results? [e.g. 9]"
read ext_res1
f_res1=$file_results$ext_res1

if [ -f parametres.in ]
then
    " we are in a directory with the parametres.in => STOP the job "
    exit
fi

cp -i $DIR1/parametres.in parametres.in
cp -i $DIR1/$f_res1 file_res1.tmp
graficsth <<END
file_res1.tmp
END
cp $fileres fileESL_res1.tmp
cp titol.tmp file_titol_res1.tmp
rm -f parametres.in file_res1.tmp

awk '{print $0}' $f_limits | read x1 x2 y1 y2 x2vel y2vel
awk '{if(NR==1) {print $4 } }' $fileres | read TIME_My
regio=$x1/$x2/$y1/$y2
regio_vel=0/$x2vel/0/$y2vel
xtit=$(($x2+0.8*$x2)) 
ytit=$(($y2-1*$x1)) 
ytitsota=$(($ytit-5*$x1)) 


ls --color=auto
echo "     "
echo " Directory with the Model 2?"
read DIR2
echo " Extension to be plotted File with the results? [e.g. 9]"
read ext_res2
f_res2=$file_results$ext_res2
cp -i $DIR2/parametres.in parametres.in
cp -i $DIR2/$f_res2 file_res2.tmp
graficsth <<END
file_res2.tmp
END
cp $fileres fileESL_res2.tmp
cp titol.tmp file_titol_res2.tmp
rm -f parametres.in file_res2.tmp

#echo 'Do you want to aplied a Gaussian filter for the data ?  1-yes'
#read Sifilter
Sifilter=5
   if [ $Sifilter -eq 1 ]
   then
        echo 'Gaussian wide in km?'
        read Gaussian
   fi

xscale=0.004
ysalt=6.5
##----------  elevation difference ----------
awk '{if($1!="#"){print $1,$2,$3 }}' fileESL_res1.tmp > file_xy_1.tmp
awk '{if($1!="#"){print $1,$2,$3 }}' fileESL_res2.tmp > file_xy_2.tmp
paste file_xy_1.tmp file_xy_2.tmp > file_xy_12.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_12.tmp 
awk '{print $1,$2,($6-$3) }' file_xy_12.tmp > file_xy.tmp

xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1

pstext file_titol_res1.tmp -P -R -X1 -Y24.8 -JX10/4.1 -N -K -V > $FILEPS
pstext file_titol_res2.tmp -R -Y-1.1 -JX -N -K -O -V >> $FILEPS
psbasemap -Y-3.6 -X1 -R -Jx$scale_4Fig -B$Bx_tics_4/$By_tics_4:" ":WNes -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi

make_polar_cpt.job <<END   ## Crea la paleta de color
file_xy.tmp
END


grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
grdcontour $file_grd -Bnsew -C0.5 -A1f6 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D15.8/2.9/5.8/.3 -B:" ": -O -K -V >> $FILEPS
pstext -R -Jx -O -K -N -V <<END>> $FILEPS
$xtit $ytit 11 0 4 2  @~D@~elevation [km]
$xtit 1200 11 0 5 2 Model2 ($DIR2)
$xtit 600 11 0 5 2 - Model1 ($DIR1)
END
rm -f file_*.tmp

##----------  Difference sediment thickness ----------
awk '{if($1!="#"){print $1,$2,$4 }}' fileESL_res1.tmp > file_xy_1.tmp
awk '{if($1!="#"){print $1,$2,$4 }}' fileESL_res2.tmp > file_xy_2.tmp
paste file_xy_1.tmp file_xy_2.tmp > file_xy_12.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_12.tmp 
awk '{print $1,$2,$6-$3 }' file_xy_12.tmp > file_xy.tmp

xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
psbasemap -Y-$ysalt -R -Jx -B$Bx_tics_4/$By_tics_4:" ":Wnes -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi

make_polar_cpt.job <<END   ## Crea la paleta de color
file_xy.tmp
END


grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
grdcontour $file_grd -Bnsew -C0.5 -A1f6 -L0.2/4 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D15.8/2.9/5.8/.3 -B:"  ": -O -K -V >> $FILEPS
pstext -R -Jx -O -K -N -V <<END>> $FILEPS
$xtit $ytit 11 0 4 2 @~D@~sediment_thickness [km]
END

rm -f file_*.tmp 

#-----  TERCERA -  gruix cortical -----
awk '{if($1!="#"){print $1,$2,$5 }}' fileESL_res1.tmp > file_xy_1.tmp
awk '{if($1!="#"){print $1,$2,$5 }}' fileESL_res2.tmp > file_xy_2.tmp
paste file_xy_1.tmp file_xy_2.tmp > file_xy_12.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_12.tmp 
awk '{print $1,$2,$6-$3 }' file_xy_12.tmp > file_xy.tmp

xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
psbasemap -Y-$ysalt -R -Jx -B$Bx_tics_4/$By_tics_4:" ":Wnes -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi

make_polar_cpt.job <<END   ## Crea la paleta de color
file_xy.tmp
END


grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
grdcontour $file_grd -Bnsew -C1 -A2f6 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D15.8/2.9/5.8/.3 -B:"  ": -O -K -V >> $FILEPS
pstext -R -Jx -O -K -N -V <<END>> $FILEPS
$xtit $ytit 12 0 4 2 @~D@~crustal_thickness  [km]
END
rm -f file_*.tmp 

##----------  QUARTA  - gruix mantell litosferic ----------
awk '{if($1!="#"){print $1,$2,$6-$4-$5 }}' fileESL_res1.tmp > file_xy_1.tmp
awk '{if($1!="#"){print $1,$2,$6-$4-$5 }}' fileESL_res2.tmp > file_xy_2.tmp
paste file_xy_1.tmp file_xy_2.tmp > file_xy_12.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_12.tmp 
awk '{print $1,$2,$6-$3 }' file_xy_12.tmp > file_xy.tmp

xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
#makecpt -Cjet -T45/100/5 > file_palette_cpt.tmp 
psbasemap -R -Y-$ysalt -Jx -B$Bx_tics_4/$By_tics_4:" ":Wnes -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi

make_polar_cpt.job <<END   ## Crea la paleta de color
file_xy.tmp
END


grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
grdcontour $file_grd -Bnsew -C5 -A10f6 -G3/8 -O -K -Jx -R -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D15.8/2.9/5.8/.3 -B:"  ": -O -K -V >> $FILEPS
pstext -R -Jx -O -N -V <<END>> $FILEPS
$xtit $ytit 12 0 4 2 @~D@~lithospheric_mantle_thickness [km]
END

fi


if [ $selec -eq 131 ]    #######  DIFFERENCES: the strain rate, viscosity ######
then
FILEPS=Difference_strain.ps

ls --color=auto
echo " Directory with the Model 1?"
read DIR1
#DIR1="Erosion"
echo DIR1 $DIR1
#echo " File with the results? [e.g. resultats9]"
#read f_res1
echo " Extension to be plotted File with the results? [e.g. 9]"
read ext_res1
f_res1=$file_results$ext_res1

if [ -f parametres.in ]
then
    " we are in a directory with the parametres.in => STOP the job "
    exit
fi

cp -i $DIR1/parametres.in parametres.in
cp -i $DIR1/$f_res1 file_res1.tmp
graficsth <<END
file_res1.tmp
END
cp $fileres fileESL_res1.tmp
cp titol.tmp file_titol_res1.tmp
cp $fcorba1 file_fcorba_DIR1.tmp
rm -f parametres.in file_res1.tmp

awk '{print $0}' $f_limits | read x1 x2 y1 y2 x2vel y2vel
awk '{if(NR==1) {print $4 } }' $fileres | read TIME_My
regio=$x1/$x2/$y1/$y2
regio_vel=0/$x2vel/0/$y2vel
xtit=$(($x2+0.8*$x2)) 
ytit=$(($y2-1*$x1)) 
ytitsota=$(($ytit-5*$x1)) 

ls --color=auto
echo "     "
echo " Directory with the Model 2?"
read DIR2
#DIR2="No_Erosion"
echo DIR2 $DIR2
#echo " File with the results? [e.g. resultats9]"
#read f_res2
echo " Extension to be plotted File with the results? [e.g. 9]"
read ext_res2
f_res2=$file_results$ext_res2

cp -i $DIR2/parametres.in parametres.in
cp -i $DIR2/$f_res2 file_res2.tmp
graficsth <<END
file_res2.tmp
END
cp $fileres fileESL_res2.tmp
cp titol.tmp file_titol_res2.tmp
cp $fcorba1 file_fcorba_DIR2.tmp
rm -f parametres.in file_res2.tmp

#echo 'Do you want to aplied a Gaussian filter for the data ?  1-yes'
#read Sifilter
Sifilter=5
   if [ $Sifilter -eq 1 ]
   then
        echo 'Gaussian wide in km?'
        read Gaussian
   fi

xscale=$scale_1Fig

cat <<END> file_palette_cpt.tmp
-0.5	0	0	80	-0.25	0	0	80
-0.25	0	0	150	-0.2	0	0	150
-0.2	57	57	220	-0.15	57	57	220
-0.15	113	113	255	-0.1	113	113	255
-0.1	170	170	255	-0.05	170	170	255
-0.05	227	227	255	0	227	227	255
0	255	227	227	0.05	255	227	227
0.05	255	170	170	0.1	255	170	170
0.1	255	113	113	0.15	255	113	113
0.15	220	57	57	0.2	220	57	57
0.2	150	0	0	0.25	150	0	0
0.25	80	0	0	0.5	80	0	0
END
makecpt -Cpolar -T-2.5/2.5/0.5 > file_palette_cpt.tmp
##----------  Difference vertical strain rate ----------					##### PANNEL 1
awk '{if($1!="#"){print $1,$2,$11*1e16 }}' fileESL_res1.tmp > file_xy_1.tmp
awk '{if($1!="#"){print $1,$2,$11*1e16 }}' fileESL_res2.tmp > file_xy_2.tmp
DATA_plot=" @~D@~vertical_strain_rate [10@+-16@+ s@+-1@+] "

##----------  Difference viscosity ----------
#awk '{if($1!="#"){print $1,$2,log($8)/log(10) }}' fileESL_res1.tmp > file_xy_1.tmp
#awk '{if($1!="#"){print $1,$2,log($8)/log(10) }}' fileESL_res2.tmp > file_xy_2.tmp
#DATA_plot=" @~D@~log(@~h@~ @-eff@-[Pa.s]) "

paste file_xy_1.tmp file_xy_2.tmp > file_xy_12.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_12.tmp 
awk '{print $1,$2,$6-$3 }' file_xy_12.tmp > file_xy.tmp

xyz2grd file_xy.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1

pstext file_titol_res1.tmp -R -Y16 -JX4.5 -N -K -V > $FILEPS
pstext file_titol_res2.tmp -R -Y-1.5 -JX -N -K -O -V >> $FILEPS
psbasemap -Y-11 -X-0.8 -R -Jx$xscale -Ba500f250:" $DIR2 - $DIR1":/a500f1250WNes -O -K -V >> $FILEPS

file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi

#grd2cpt  $file_grd > file_palette_cpt.tmp
#make_polar_cpt.job <<END   ## Crea la paleta de color
#file_xy.tmp
#END

grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psxy $fcorba1 -R -O -K -M -L -Jx -W5/0/0/0t35_5_0_0:5 -V >> $FILEPS
#grdcontour $file_grd -Bnsew -C0.5 -A1f6 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
#psscale -Cfile_palette_cpt.tmp -L -D15.8/2.9/5.8/.3 -B:" ": -O -K -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D6/-0.4/12.6/.3h -B:" $DATA_plot ": -O -K -V >> $FILEPS

##----------  Difference viscosity ----------							##### PANNEL 2
awk '{if($1!="#"){print $1,$2,log($8)/log(10) }}' fileESL_res1.tmp > file_xy_1.tmp
awk '{if($1!="#"){print $1,$2,log($8)/log(10) }}' fileESL_res2.tmp > file_xy_2.tmp
DATA_plot=" @~D@~log(@~h@~ @-eff@-[Pa.s]) "

##----------  Difference Total Strain Accumulation ----------
#awk '{if($1!="#"){print $1/1e3,$2/1e3,$3 } }'  $DIR1/res_TotalStrain$ext_res1 > file_xy_1.tmp	# km
#awk '{if($1!="#"){print $1/1e3,$2/1e3,$3 } }'  $DIR2/res_TotalStrain$ext_res2 > file_xy_2.tmp	# km
#DATA_plot=" @~D@~ Total Vertical Strain Accumulation "

paste file_xy_1.tmp file_xy_2.tmp > file_xy_12.tmp
awk '{if($1!=$4 || $2!=$5) {print "Ojo, fitxers mal ordenats",$1,$4,$2,$5} }' file_xy_12.tmp 
awk '{print $1,$2,$6-$3 }' file_xy_12.tmp > file_xy.tmp
xyz2grd file_xy.tmp -R -Gfile_grd.tmp -I$x1/$y1
#grd2cpt file_grd.tmp > file_palette_cpt.tmp
makecpt -Cpolar -T-0.2/0.2/0.05 > file_palette_cpt.tmp
#makecpt -Chaxby -T-0.1/0.1/.01 > file_palette_cpt.tmp
#make_polar_cpt.job <<END   ## Crea la paleta de color
#file_xy.tmp
#END

psbasemap -X14.2 -R -Jx -Ba500f250:" $DIR2 - $DIR1":/a500f1250WNes -K -O -V >> $FILEPS
file_grd=file_grd.tmp
   if [ $Sifilter -eq 1 ]
   then
	grdfilter file_grd.tmp -D0 -Fg$Gaussian -Gfile_grd_filt.tmp -V
	file_grd=file_grd_filt.tmp
   fi

#grd2cpt  $file_grd > file_palette_cpt.tmp
#make_polar_cpt.job <<END   ## Crea la paleta de color
#file_xy.tmp
#END

grdimage $file_grd -Jx -R -O -K -Cfile_palette_cpt.tmp -V >> $FILEPS
psxy file_fcorba_DIR1.tmp -R -O -K -M -L -Jx -W5/0/0/0ta -V >> $FILEPS
psxy file_fcorba_DIR2.tmp -R -O -K -M -L -Jx -W5/100/100/100to -V >> $FILEPS
grdcontour $file_grd -Bnsew -C0.1 -A0.2f6 -L0.2/4 -G2.5/8 -O -K -Jx -R -V >> $FILEPS
#psscale -Cfile_palette_cpt.tmp -L -D15.8/2.9/5.8/.3 -B:"  ": -O -K -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D6/-1/12.6/.25h -B:" $DATA_plot ": -O -V >> $FILEPS
#pstext -R -Jx -O -N -V <<END>> $FILEPS
#$xtit $ytit 11 0 4 2 @~D@~log(@~h@~ @-eff@-[Pa.s])
#END

fi


if [ $selec -eq 135 ]
then

echo "Correr primer el output_uhuru.job (opcio 126) a cada directori K*/"
echo " per crear els fitxers: *.t (elevation, crust i litosferic mantle"

DIR0=0
DIR1=100
DIR2=200
DIR3=300
#DIR4=400
DIR5=500
DIR6=1000
DIR7=2000
DIR8=3000
file_e=elevation.t
file_s=crust.t
file_lm=lithospheric_mantle.t
FILEPS=Delta_var_K.ps

echo "time?"
read time

DIR=$DIR0
echo "directory " K$DIR
f_elev=K$DIR/$file_e
f_crust=K$DIR/$file_s
f_mant=K$DIR/$file_lm             # Kerosdiff, var maxim, var minim
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_elev > file_K_e.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_crust > file_K_s.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_mant > file_K_lm.tmp
 
awk '{if($1==0.2) {print DIR,$2-$5} }' DIR=$DIR $f_elev > file_K_e_0.tmp  
awk '{if($1==0.2) {print DIR,$2-$5} }' DIR=$DIR $f_crust > file_K_s_0.tmp  
awk '{if($1==0.2) {print DIR,$2-$5} }' DIR=$DIR $f_mant > file_K_lm_0.tmp  


DIR=$DIR1
echo "directory " K$DIR
f_elev=K$DIR/$file_e
f_crust=K$DIR/$file_s
f_mant=K$DIR/$file_lm
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_elev >> file_K_e.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_crust >> file_K_s.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_mant >> file_K_lm.tmp

DIR=$DIR2
echo "directory " K$DIR
f_elev=K$DIR/$file_e
f_crust=K$DIR/$file_s
f_mant=K$DIR/$file_lm
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_elev >> file_K_e.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_crust >> file_K_s.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_mant >> file_K_lm.tmp

DIR=$DIR3
echo "directory " K$DIR
f_elev=K$DIR/$file_e
f_crust=K$DIR/$file_s
f_mant=K$DIR/$file_lm
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_elev >> file_K_e.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_crust >> file_K_s.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_mant >> file_K_lm.tmp

DIR=$DIR4
echo "directory " K$DIR
f_elev=K$DIR/$file_e
f_crust=K$DIR/$file_s
f_mant=K$DIR/$file_lm
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_elev >> file_K_e.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_crust >> file_K_s.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_mant >> file_K_lm.tmp

DIR=$DIR5
echo "directory " K$DIR
f_elev=K$DIR/$file_e
f_crust=K$DIR/$file_s
f_mant=K$DIR/$file_lm
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_elev >> file_K_e.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_crust >> file_K_s.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_mant >> file_K_lm.tmp

DIR=$DIR6
echo "directory " K$DIR
f_elev=K$DIR/$file_e
f_crust=K$DIR/$file_s
f_mant=K$DIR/$file_lm
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_elev >> file_K_e.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_crust >> file_K_s.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_mant >> file_K_lm.tmp

DIR=$DIR7
echo "directory " K$DIR
f_elev=K$DIR/$file_e
f_crust=K$DIR/$file_s
f_mant=K$DIR/$file_lm
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_elev >> file_K_e.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_crust >> file_K_s.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_mant >> file_K_lm.tmp

DIR=$DIR8
echo "directory " K$DIR
f_elev=K$DIR/$file_e
f_crust=K$DIR/$file_s
f_mant=K$DIR/$file_lm
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_elev >> file_K_e.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_crust >> file_K_s.tmp
awk '{if($1==time) {print DIR,$2,$5} }' time=$time DIR=$DIR $f_mant >> file_K_lm.tmp


scale=15/5.5
y_salt=-7
K1=0
K2=4000
awk '{print $1,$2-$3}' file_K_e.tmp > file_K_Delta.tmp	# Delta elevation
sort -k 2,2nr -o file_ord.tmp file_K_Delta.tmp 
wc file_K_Delta.tmp | read nrow a b c
awk '{if(NR==1){print $2 }}' file_ord.tmp | read De_max 
awk '{if(NR==nrow){print $2 }}' nrow=$nrow file_ord.tmp | read De_min 
Regio=$K1/$K2/1/6
#Regio=$K1/$K2/$De_min/$De_max                                
echo "Regio:", $Regio
psbasemap -P -Y21 -R$Regio -JX$scale -Ba500f250:"K (erosdif) [m@+2@+/a] ":/1f0.5:"  \
        @~D@~elevation [km] ":WSne -V -K > $FILEPS
psxy file_K_Delta.tmp -R -JX -W4 -N -K -O -V >> $FILEPS
psxy file_K_Delta.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
#awk '{print $1,$2}' file_K_e.tmp > file_K_emax.tmp
#psxy file_K_emax.tmp -R -Ba/1f0.5:" elevation [km] ":E -JX -Sc0.1 -G0 -N -K -O -V >> $FILEPS
#awk '{print $1,$3}' file_K_e.tmp > file_K_emin.tmp
#psxy file_K_emin.tmp -R -JX -Sc0.1 -G0 -N -K -O -V >> $FILEPS

psxy file_K_e_0.tmp -R -JX -Ss0.3 -G200 -N -K -O -V >> $FILEPS
pstext -R -JX -O -K -N -V <<END>> $FILEPS
$K2 $De_max 12 0 4 3 $time Myear
END

awk '{print $1,$2-$3}' file_K_s.tmp > file_K_Delta.tmp	# Delta crust
sort -k 2,2nr -o file_ord.tmp file_K_Delta.tmp 
wc file_K_Delta.tmp | read nrow a b c
awk '{if(NR==1){print $2 }}' file_ord.tmp | read Ds_max 
awk '{if(NR==nrow){print $2 }}' nrow=$nrow file_ord.tmp | read Ds_min 
Regio=$K1/$K2/24/34                           
#Regio=$K1/$K2/$Ds_min/$Ds_max                                
echo "Regio:", $Regio
psbasemap -Y$y_salt -R$Regio -JX -Ba500f250:"K (erosdif) [m@+2@+/a] ":/1f0.5:" \
        @~D@~crust [km]":WSen -V -K -O >> $FILEPS
psxy file_K_Delta.tmp -R -JX -W4 -N -K -O -V >> $FILEPS
psxy file_K_Delta.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
psxy file_K_s_0.tmp -R -JX -Ss0.3 -G200 -N -K -O -V >> $FILEPS

awk '{print $1,$2-$3}' file_K_lm.tmp > file_K_Delta.tmp	# Delta lithospheric mantle
sort -k 2,2nr -o file_ord.tmp file_K_Delta.tmp 
wc file_K_Delta.tmp | read nrow a b c
awk '{if(NR==1){print $2 }}' file_ord.tmp | read Dlm_max 
awk '{if(NR==nrow){print $2 }}' nrow=$nrow file_ord.tmp | read Dlm_min 
Regio=$K1/$K2/20/48                               
#Regio=$K1/$K2/$Dlm_min/$Dlm_max                                
echo "Regio:", $Regio
psbasemap -Y$y_salt -R$Regio -JX -Ba500f250:"K (erosdif) [m@+2@+/a] ":/2f1:" \
        @~D@~lith.mantle [km]":WSen -V -K -O >> $FILEPS
#psxy file_K_lm_0.tmp -R -JX -Ss0.3 -G200 -N -K -O -V >> $FILEPS
psxy file_K_Delta.tmp -R -JX -W4 -N -O -K -V >> $FILEPS
psxy file_K_Delta.tmp -R -JX -Sc0.1 -N -O -V >> $FILEPS

fi


if [ $selec -eq 136 ]
then

echo "Correr primer el output_uhuru.job (opcio 126) a cada directori */"
echo " per crear els fitxers: *.t (elevation, crust i litosferic mantle"

DIR1=MS34
DIR2=MS35
DIR3=MS36
DIR4=MS37
DIR5=MS38
FILEPS=Delta_temps.ps

file_e=elevation.t
file_s=crust.t
file_lm=lithospheric_mantle.t
file_ezz=VerticalStrainRate.t
W1=3
W2=3/200/0/0
W3=3/0/0/200
W4=3/0/200/0
W5=3/250/0/250

scale=15/5.5
y_salt=-7
Time_max=65
Regio=0/$Time_max/0/6					##### Delta elevation
BaT=a5f2.5
psbasemap -P -Y23 -R$Regio -JX$scale -B$BaT:"Time [My]":/1f0.5:"  \
        @~D@~elevation [km] ":WSnE -V -K > $FILEPS
pstext -R -JX -O -K -N -V <<END>> $FILEPS
0 12 10 0 4 1 $DIR_main
END
awk '{if(NR==2) {print $1,$2-$5} }' $DIR1/$file_e | read xe0 ye0
psxy -R -JX -W1/170 -N -K -O -V <<END>> $FILEPS
$xe0 $ye0
$Time_max $ye0
END

DIR=$DIR1
awk '{if($1!="#") {print $1,$2-$5} }' $DIR/$file_e > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W1 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp > file_text.tmp
rm -f file_t_D.tmp
DIR=$DIR2
awk '{if($1!="#") {print $1,$2-$5} }' $DIR/$file_e > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W2 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp
rm -f file_t_D.tmp
DIR=$DIR3
awk '{if($1!="#") {print $1,$2-$5} }' $DIR/$file_e > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W3 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp
rm -f file_t_D.tmp
DIR=$DIR4
awk '{if($1!="#") {print $1,$2-$5} }' $DIR/$file_e > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W4 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp
rm -f file_t_D.tmp
DIR=$DIR5
awk '{if($1!="#") {print $1,$2-$5} }' $DIR/$file_e > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W5 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp
rm -f file_t_D.tmp

pstext file_text.tmp -R -JX -O -K -N -V >> $FILEPS

Regio=0/$Time_max/0/56		# 27/36					##### Delta crustal thickness
psbasemap -Y$y_salt -R$Regio -JX -B$BaT:"Time [My]":/10f5:" \
        @~D@~crust [km]":WSEn -V -K -O >> $FILEPS
awk '{if(NR==2) {print $1,$2-$5} }' $DIR1/$file_s | read xe0 ye0
psxy -R -JX -W1/170 -N -K -O -V <<END>> $FILEPS
$xe0 $ye0
$Time_max $ye0
END
DIR=$DIR1
awk '{print $1,$2-$5}' $DIR/$file_s > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W1 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
DIR=$DIR2
awk '{print $1,$2-$5}' $DIR/$file_s > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W2 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
DIR=$DIR3
awk '{print $1,$2-$5}' $DIR/$file_s > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W3 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
DIR=$DIR4
awk '{print $1,$2-$5}' $DIR/$file_s > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W4 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
DIR=$DIR5
awk '{print $1,$2-$5}' $DIR/$file_s > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W5 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS


Regio=0/$Time_max/0/140					##### Delta lithospheric mantle thickness
psbasemap -Y$y_salt -R$Regio -JX -B$BaT:"Time [My]":/20f10:" \
        @~D@~lith.mantle [km]":WSEn -V -K -O >> $FILEPS
awk '{if(NR==2) {print $1,$2-$5} }' $DIR1/$file_lm | read xe0 ye0
psxy -R -JX -W1/170 -N -K -O -V <<END>> $FILEPS
$xe0 $ye0
$Time_max $ye0
END
DIR=$DIR1
awk '{print $1,$2-$5}' $DIR/$file_lm > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W1 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
DIR=$DIR2
awk '{print $1,$2-$5}' $DIR/$file_lm > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W2 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
DIR=$DIR3
awk '{print $1,$2-$5}' $DIR/$file_lm > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W3 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -O -K -V >> $FILEPS
DIR=$DIR4
awk '{print $1,$2-$5}' $DIR/$file_lm > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W4 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -O -K -V >> $FILEPS
DIR=$DIR5
awk '{print $1,$2-$5}' $DIR/$file_lm > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W5 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS


Regio=0/$Time_max/4/13					##### Maximum vertical strain rate
psbasemap -Y$y_salt -R$Regio -JX -B$BaT:"Time [My]":/2f1:" \
        max @~e@~@+.@+@-zz@- [10@+-16@+ s@+-1@+]":WSn -V -K -O >> $FILEPS
awk '{if(NR==2) {print $1,$2-$5} }' $DIR1/$file_ezz | read xe0 ye0
psxy -R -JX -W1/170 -N -K -O -V <<END>> $FILEPS
$xe0 $ye0
$Time_max $ye0
END
DIR=$DIR1
awk '{print $1,$2}' $DIR/$file_ezz > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W1 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp > file_text.tmp
DIR=$DIR2
awk '{print $1,$2}' $DIR/$file_ezz > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W2 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp
DIR=$DIR3
awk '{print $1,$2}' $DIR/$file_ezz > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W3 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -O -K -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp
DIR=$DIR4
awk '{print $1,$2}' $DIR/$file_ezz > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W4 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -O -K -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp
DIR=$DIR5
awk '{print $1,$2}' $DIR/$file_ezz > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W5 -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp

pstext file_text.tmp -R -JX -O -K -N -V >> $FILEPS


Regio=0/$Time_max/-4/0					##### Minimum vertical strain rate. Same pannel, dashed lines
psbasemap -R$Regio -JX -B$BaT:"Time [My]":/1f0.5:" \
        min @~e@~@+.@+@-zz@- [10@+-16@+ s@+-1@+] (dashed)":E -V -K -O >> $FILEPS
dashed="ta"
DIR=$DIR1
awk '{print $1,$5}' $DIR/$file_ezz > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W1$dashed -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp > file_text.tmp
DIR=$DIR2
awk '{print $1,$5}' $DIR/$file_ezz > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W2$dashed -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp
DIR=$DIR3
awk '{print $1,$5}' $DIR/$file_ezz > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W3$dashed -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -O -K -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp
DIR=$DIR4
awk '{print $1,$5}' $DIR/$file_ezz > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W4$dashed -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -O -K -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp
DIR=$DIR5
awk '{print $1,$5}' $DIR/$file_ezz > file_t_D.tmp
psxy file_t_D.tmp -R -JX -W$W5$dashed -N -K -O -V >> $FILEPS
psxy file_t_D.tmp -R -JX -Sc0.1 -N -K -O -V >> $FILEPS
wc file_t_D.tmp | read nrow a b c
awk '{if(NR==nrow) {print $1,$2,9,0,4,1,DIR } }' nrow=$nrow DIR=$DIR file_t_D.tmp >> file_text.tmp

pstext file_text.tmp -R -JX -O -N -V >> $FILEPS

fi



if [ $selec -eq 140 ]		### Crear el fitxer  $file_rot  amb selec=21
then
FILEPS=Rotation_time.ps
echo 'point to be plotted'
read Punt
#Punt=114
file_rot=Rotation_time_P$Punt.xy
DIR1=No_Remove
DIR2=Remove
DIR3=Remove_180_Time35
R_rot=0/60/0/85

cp $DIR1/$file_rot file_rotation1.tmp
cp $DIR2/$file_rot file_rotation2.tmp
cp $DIR3/$file_rot file_rotation3.tmp

psxy file_rotation1.tmp -R$R_rot -JX10/7 -Ba5f1:"time (my)- Punt $Punt":/a10f5g30:"rotations (degrees)":nSeW -W5/0/0/0 -K -V > $FILEPS
psxy -R -JX -W1/170 -M -K -O -V <<END>> $FILEPS
35 0
35 100
>
45 0
45 100
END
psxy file_rotation2.tmp -R -JX -W5/150/0/0 -K -O -V >> $FILEPS
psxy file_rotation3.tmp -R -JX -W5/0/0/150 -O -V >> $FILEPS

fi

if [ $selec -eq 141 ]		
then
FILEPS=Delta_Rotations.ps
DIR1=No_Remove
DIR2=Remove_180_Time35
rm $FILEPS
echo Directorio 1
ls $DIR1/res_rotation*
echo Extension to be plotted $f_rotation
read ext_res


cp -i $DIR1/parametres.in parametres.in
cp -i $DIR1/$file_results$ext_res file_res1.tmp
graficsth <<END
file_res1.tmp
END
rm parametres.in

awk '{print $0}' $f_limits | read x1 x2 y1 y2 x2vel y2vel 
awk '{if(NR==1) {print $4 } }' $fileres | read TIME_My

regio=$x1/$x2/$y1/$y2
regio_vel=0/$x2vel/0/$y2vel
Regio_graf=$regio_vel		## Alps: 0/1200/0/800	# $regio_vel

echo "  "
echo regio = $regio
echo regio_vel = $regio_vel
echo Regio_graf = $Regio_graf


file_rot1=$DIR1/$f_rotation$ext_res
awk '{if($1=="#") {print $0 } }' $file_rot1
echo "   "
awk '{if($1!="#") {print $0 } }' $file_rot1 > file_rot1.tmp
wc file_rot1.tmp | read row1 a b c
echo LINES $row1

echo Directorio 2
ls $DIR2/res_rotation*
echo Extension to be plotted $f_rotation
read ext_res
file_rot2=$DIR2/$f_rotation$ext_res
awk '{if($1=="#") {print $0 } }' $file_rot2
echo "   "
awk '{if($1!="#") {print $0 } }' $file_rot2 > file_rot2.tmp
wc file_rot2.tmp | read row2 a b c
echo LINES $row2
echo "  "
echo " Are both files from the same time step?"
awk '{if($1=="#") {print $0 } }' $file_rot1
awk '{if($1=="#") {print $0 } }' $file_rot2
echo " and do they have the same number of lines?"
echo LINES DIR1 $row1         
echo LINES DIR2 $row2
read

paste file_rot1.tmp file_rot2.tmp > file_rot-paste.tmp
awk '{print ($1+$4)/2, ($2+$5)/2,  $6-$3 }' file_rot-paste.tmp > file_Delta-rot.tmp
awk '{print $1, $2}' file_rot-paste.tmp > file_xy1.tmp
awk '{print $4, $5}' file_rot-paste.tmp > file_xy2.tmp

Regio_graf=0/8000/1500/6000
psbasemap -Y4 -R$Regio_graf -Jx$scale_1Fig -B$Bx_tics:" $DIR2- $DIR1 ":/$By_tics:" km ":WeSn -V -K > $FILEPS

psbasemap -R -Jx -B$Bx_tics:" $DIR_main ":N -V -K -O >> $FILEPS

awk '{if($1!="#"){print $1,$2,$3} }' $fileres > file_xyz.tmp		## elevation
cp filePalette_cpt_color2.tmp file_cpt.tmp
xyz2grd file_xyz.tmp -R$regio_vel -Gfile_grd.tmp -I$x1/$y1
#grdimage file_grd.tmp -Jx -R$Regio_graf -Cfile_cpt.tmp -O -K -V >> $FILEPS
#psscale -Cfile_cpt.tmp -L -D18/1.7/8/.3 -B:" km ": -O -K -V >> $FILEPS
psxy $fcorba1 -R$Regio_graf -M -L -Jx -W8/100/100/100 -O -K -V >> $FILEPS
grdcontour file_grd.tmp -C0.5 -A1f8 -G30/8 -W1/100 -Jx -R -O -K -V >> $FILEPS

#awk '{if($1!="#") {print $1,$2,$3 } }' $file_rot > file_zxy.tmp
rotation_min=0		### Just plot the rotations grater than rotation_min
awk '{if($1!="#" && ($3<-0 || $3>0)) {print $1,$2,$3 } }' file_Delta-rot.tmp > file_zxy.tmp
awk '{printf " %10.2f %10.2f %2i %2i %2i %2i %4i \n ", $1,$2,10,0,1,6,$3 }' file_zxy.tmp > file_Timetext.tmp

cat <<END> file_palette_cpt.tmp
-50	0	0	120	-20	0	0	120
-20	0	0	200	-15	0	0	200
-15	60	60	220	-10	60	60	220
-10	120	120	255	-5	120	120	255
-5	180	180	255	0	180	180	255
0	255	170	170	5	255	170	170
5	255	113	113	10	255	113	113
10	220	57	57	15	220	57	57
15	200	0	0	20	200	0	0
20	120	0	0	50	120	0	0
END
psxy file_zxy.tmp -R -Bnsew -Jx -Sc0.6 -Cfile_palette_cpt.tmp -W5/255 -N -O -K -V >> $FILEPS
psscale -Cfile_palette_cpt.tmp -L -D8/-1.5/15/.3h -B:" DIR2 - DIR1": -O -K -V >> $FILEPS

#psxy file_xy1.tmp -R -Bnsew -Jx -Sc0.1 -W2/0 -N -O -K -V >> $FILEPS
#psxy file_xy2.tmp -R -Bnsew -Jx -Sc0.1 -W2/255/0/0 -N -O -K -V >> $FILEPS

pstext file_Timetext.tmp -N -R -Jx -O -V >> $FILEPS
#pstext -R -Jx -O -N -V <<END>> $FILEPS
#$y2vel -110 12 0 0 2  $contour_text
#END




fi

###########  CREA UN JPG  #################################
echo ' convert to jpg?	1 - Yes  '
echo '			2 - No    '
#read Optjpg
Optjpg=1

if [ $Optjpg -eq 1 ]
then


if [ $selec -eq 17 ]
then
   Yjpg=90
else
   Yjpg=0 
fi
 
if [ $ext_res -le 9 ]
then
   convert -trim -density 130 -rotate $Yjpg $FILEPS $FILEPS"0"$ext_res".jpg"
##   #convert $FILEPS $FILEPS"0"$ext_res".jpg"
else
   convert -trim -density 130 -rotate $Yjpg $FILEPS $FILEPS$ext_res".jpg"
##   #convert $FILEPS $FILEPS$ext_res".jpg"
fi

fi
############################################################ 

if [ $Optjpg -eq 2 ]
then 
   evince $FILEPS &
fi

rm -f file*.tmp
rm -f titol.tmp TITOL.tmp Points.tmp
rm -f $fileres $fepunt12 $fprin_stress $fvelocity $f_limits $f_resGLit $fcorba1 $fcorba2

